Consider the following Java program. Create and use method where necessary.
//program 1
import java.util.Scanner;
public class tambahNombor {
public static void main(String[] args) {
int
N1,N2,hasiltambah;
Scanner
input= new Scanner(System.in);
System.out.println("Sila
masukkan dua nombor");
N1=
input.nextInt();
N2=
input.nextInt();
hasiltambah=N1+N2;
System.out.println("Hasil
tambah dua nombor ialah "+hasiltambah);
System.out.println("Sila
masukkan dua nombor");
N1=
input.nextInt();
N2=
input.nextInt();
hasiltambah=N1+N2;
System.out.println("Hasil
tambah dua nombor ialah "+hasiltambah);
System.out.println("Sila
masukkan dua nombor");
N1=
input.nextInt();
N2=
input.nextInt();
hasiltambah=N1+N2;
System.out.println("Hasil
tambah dua nombor ialah "+hasiltambah);
System.out.println("Sila
masukkan dua nombor");
N1=
input.nextInt();
N2=
input.nextInt();
hasiltambah=N1+N2;
System.out.println("Hasil
tambah dua nombor ialah "+hasiltambah);
}
}
//program 2
import
java.util.Scanner;
public class sumInteger {
public
static void main(String[] args) {
int N1,N2,sum,count;
Scanner input = new
Scanner(System.in);
System.out.println("Sila
masukkan dua nombor!");
N1= input.nextInt();
N2= input.nextInt();
sum=0;
for(count=N1;count<=N2;count++)
{
sum=sum+count;
}
System.out.println("Hasil tambah "+N1+" hingga
"+N2+" ialah " + sum);
System.out.println("Sila masukkan dua nombor ");
N1= input.nextInt();
N2= input.nextInt();
sum=0;
for(count=N1;count<=N2;count++)
{
sum=sum+count;
}
System.out.println("Hasil tambah "+N1+" hingga
"+N2+" ialah " + sum);
System.out.println("Sila masukkan dua nombor!");
N1= input.nextInt();
N2= input.nextInt();
sum=0;
for(count=N1;count<=N2;count++)
{
sum=sum+count;
}
System.out.println("Hasil tambah "+N1+" hingga
"+N2+" ialah " + sum);
}
}
//program 3
import java.util.Scanner;
public class bmi {
public static void main(String[] args) {
double
berat,tinggi,BMI;
String
status;
Scanner
input = new Scanner(System.in);
System.out.println("Sila
masukkan berat (KG) dan tinggi (Meter)");
berat
= input.nextDouble();
tinggi
= input.nextDouble();
BMI=berat/(tinggi*tinggi);
if(BMI<18 .5="" p="">
System.out.println("underweight");
else
if((BMI>=18.5)&&(BMI<=24.9))
System.out.println("healthy");
else
if((BMI>=25)&&(BMI<=29.9))
System.out.println("overweight");
else
if (BMI>=30)
System.out.println("obes");
System.out.println("Sila
masukkan berat (KG) dan tinggi (Meter)");
berat
= input.nextDouble();
tinggi
= input.nextDouble();
BMI=berat/(tinggi*tinggi);
if(BMI<18 .5="" p="">
18>
System.out.println("underweight");
else
if((BMI>=18.5)&&(BMI<=24.9))
System.out.println("healthy");
else
if((BMI>=25)&&(BMI<=29.9))
System.out.println("overweight");
else
if (BMI>=30)
System.out.println("obes");
System.out.println("Sila
masukkan berat (KG) dan tinggi (Meter)");
berat
= input.nextDouble();
tinggi
= input.nextDouble();
BMI=berat/(tinggi*tinggi);
if(BMI<18 .5="" p="">
18>
18>
System.out.println("underweight");
else
if((BMI>=18.5)&&(BMI<=24.9))
System.out.println("healthy");
else
if((BMI>=25)&&(BMI<=29.9))
System.out.println("overweight");
else
if (BMI>=30)
System.out.println("obes");
}
}
//program 4
import java.util.Scanner;
public class ulangPesanan {
public static void main(String[] args) {
Scanner
input = new Scanner(System.in);
String
pesanan;
int
i,jumlahUlangan;
System.out.println("Sila
masukkan Pesanan :");
pesanan
= input.nextLine();
System.out.println("Berapa
kali nak ulang ? :");
jumlahUlangan
= input.nextInt();
for (i=1;i<=jumlahUlangan;i++)
{
System.out.println(pesanan);
}
System.out.println("Sila masukkan
Pesanan :");
pesanan
= input.nextLine();
System.out.println("Berapa
kali nak ulang ? :");
jumlahUlangan
= input.nextInt();
for (i=1;i<=jumlahUlangan;i++)
{
System.out.println(pesanan);
}
System.out.println("Sila masukkan
Pesanan :");
pesanan
= input.nextLine();
System.out.println("Berapa
kali nak ulang ? :");
jumlahUlangan
= input.nextInt();
for (i=1;i<=jumlahUlangan;i++)
{
System.out.println(pesanan);
}
}
}
18>
No comments:
Post a Comment