SpotFlow: Tracking Method Calls and States at Runtime
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
1. SET INDUKSI
APAKAH FUNGSI PENYATAAN TERSEBUT DALAM
ATURCARA?
1. System.out.println
2.//
3.int umur;
4.float gaji;
5.public class ScanLine
6.{
7.}
2. Analisis aturcara dibawah
public class InputExp {
public static void main(String[] args) {
int nom1;
int nom2;
hasilDarab;
nom1=3, nom2=9;
hasilDarab=nom1*nom2;
System.out.println (“Hasil Darab nom1 dan nom2 = ” +hasilDarab);
3. Objektif Pembelajaran
Pelajar dapat:
1. Menulis aturcara bagi menerima input daripada pengguna dari
console
2. Mengetahui fungsi Method nextInt, nextDouble, nextLine, nextFloat
dalam kelas Scanner;
3. Tahu membezakan penggunaan jenis data bagi setiap pembolehubah.
5. Kelas Scanner
Kelas Scanner merupakan kelas dalam java.util.
Ia membenarkan pengguna memasukkan pelbagai jenis nilai
seperti integer, nombor perpuluhan ataupun teks
Nilai dimasukkan oleh pengguna dalam console
Arahan berikut diperlukan :
1. import java.util.Scanner;
2. Scanner input = new Scanner(System.in);
JOM BINA ATURCARA
7. Analogi (masuk input dari Pengguna)
System.in
Scanner input=
new Scanner
input.nextLine
String nama=
input.nextLine();
8. Jenis Data method Function
Int nextInt () Returns the next token as an int. If the next token is not an integer,
InputMismatchException is thrown.
Long nextLong() Returns the next token as a long. If the next token is not an integer,
InputMismatchException is thrown.
Float nextFloat () Returns the next token as a float. If the next token is not a float or is out
of range, InputMismatchException is thrown.
Double nextDouble () Returns the next token as a long. If the next token is not a float or is out
of range, InputMismatchException is thrown.
String Next () Finds and returns the next complete token from this scanner and returns
it as a string; a token is usually ended by whitespace such as a blank or
line break. If not token exists,NoSuchElementException is thrown.
String NextLine () Returns the rest of the current line, excluding any line separator at the
end.
void Close () Closes the scanner.
9. Contoh Penggunaan Method Scanner
int number = in.nextInt();
float real = in.nextFloat();
long number2 = in.nextLong();
double real2 = in.nextDouble();
String string = in.next();
10. Contoh aturcara 1
import java.util.Scanner;
public class InputExp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Reads a single line from the console
// and stores into name variable
System.out.println (“Please Key In Your Name”);
String name = in.nextLine();
// Reads a integer from the console
// and stores into age variable
System.out.println (“Please Key In Your Age”);
int age=in.nextInt();
// Prints name and age to the console
System.out.println("Name :"+name);
System.out.println("Age :"+age);
}
}
12. Aturcara 3
import java.util.Scanner;
public class ScanLine {
public static void main(String[] args) {
// create a scanner object
Scanner scanner = new Scanner(System.in);
// input an int from the user
System.out.println("Enter an int");
int i = scanner.nextInt();
// now, try to input a whole line from the user
System.out.println("Enter a sentence -- spaces are allowed");
String s1 = scanner.nextLine();
// input a string from the user
System.out.println("Enter a String -- with no spaces");
String s2 = scanner.nextLine();
// output data to the user
System.out.println("i = " + i);
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
}
}
13. Aturcara 4
import java.util.Scanner;
import java.io.*; // Access System.out
public class NumericInput
{
public static void main(String[] args)
{
// Declarations
Scanner in = new Scanner(System.in);
int integer;
long longInteger;
float realNumber;
double doubleReal;
String string1;
String string2;
// Prompts
System.out.println("Enter an integer, a long integer, "
+ "a floating-point ");
System.out.println("number, another floating-point number, "
+ "and a string.");
System.out.println("Separate each with a blank or enter.");
// Read in values
integer = in.nextInt();
longInteger = in.nextLong();
realNumber = in.nextFloat();
doubleReal = in.nextDouble();
string1 = in.nextLine();
System.out.println("Now enter another value.");
string2 = in.next();
System.out.println("Here is what you entered: ");
System.out.println(integer + " " + longInteger + " " +
realNumber + " " + doubleReal + " " + string1 +" and " + string2);
}
}
14. Output:
Enter an integer, a long integer, a floating-point
number, another floating-point number, and a string.
Separate each with a blank or enter.
23
24
25.0 233333333333333.444 Hello
Now enter another value.
23.4
Here is what you entered:
23 24 25.0 2.3333333333333344E14 Hello and 23.4
15. LATIHAN
1. Anda dikehendaki membina aturcara bagi mengira hasil tambah 3 nombor yang
dimasukkan oleh pengguna.
2. Anda dikehendaki membina aturcara bagi mengira hasil darab 3 nombor yang
dimasukkan oleh pengguna.
3. Anda dikehendaki membina aturcara bagi mengira purata 5 markah pelajar. Markah
tersebut merupakan markah kuiz, tugasan, amali, pembentangan dan ujian.
4. Anda dikehendaki membina aturcara dengan meminta input nama, no telefon, umur,
bilangan adik beradik dan pendapatan penjaga. Output kan semula maklumat yang
telah dimasukkan.
5. Anda dikehendaki membina aturcara bagi mengira keluasan padang bola sepak yang
berbentuk segiempat tepat.
6. Anda dikehendaki membina aturcara mengira isipadu satu tin minuman. Gunakan
pernyataan berikut bagi Pemalar (Constant): final double pie =3.142
p/s: gunakan algoritma pseudokod, carta alir untuk anda membantu anda membuat aturcara