Pengenalan Kelas
Kemahiran perlu diperolehi:
1.Copy and paste fail berikut dan savekan sebagai "Kertas.java".
/* program Java menunjukkan penggunaan class : 1:57 PM 19-Jul-98 */
class Segi4Tepat
{
Segi4Tepat(int s1, int s2) // ini ialah constructor
{
sisi1 = s1;
sisi2 = s2;
}
int ukurLilit() // ini method
{
return 2 * sisi1 + 2 * sisi2;
}
double luas() // ini method
{
return (double)sisi1 * sisi2;
}
private int sisi1; // ini data
private int sisi2; // ini data
} // tamat kelas Segi4Tepat
class Kertas
{
public static void main(String args[])
{
Segi4Tepat kertasA4;
kertasA4 = new Segi4Tepat(8, 11);
System.out.println("Ukur lilit kertas A4: " + kertasA4.ukurLilit());
System.out.println("Luas kertas A4: " + kertasA4.luas());
Segi4Tepat kertasB5 = new Segi4Tepat(7, 10);
System.out.println("Ukur lilit kertas B5: " + kertasB5.ukurLilit());
System.out.println("Luas kertas B5: " + kertasB5.luas());
// System.out.println("Sisi 1 B5: " + kertasB5.sisi1); /* ralat! kenapa?*/
} // tamat main
} // tamat kelas Kertas
2.Kompil aturcara anda: Pada DOS prompt, taipkan
javac Kertas.java
Jika tiada ralat, tiada mesej diberikan.
3.Laksanakan aturcara anda: Taipkan pula
java Kertas
Anda mendapat output seperti:
Ukur lilit kertas A4: 38
Luas kertas A4: 88.0
Ukur lilit kertas B5: 34
Luas kertas B5: 70.0
Jawab soalan-soalan berikut:
sisi1
"
dari dalam main
?sisi2
"
dari dalam main
?pintu
" dari kelas Segi4Tepat
dengan sisi1 bernilai 5 dan sisi2 bernilai 13.pintu
"
Segi4Tepat
kertasA4; kertasA4 = new Segi4Tepat(8, 11);
Segi4Tepat
kertasA4 = new Segi4Tepat(8, 11);
Latihan tambahan: -- buat satu method baru
(tambahan), "potongSisi1
"
yang memotong sisi1 sebanyak n inci. Contoh penggunaan method itu ialah:
kertasB5.potongSisi1(2);
yang bermakna:
"potong sisi1 kertasB5 sebanyak 2 inci".
-- Tamat sesi makmal -- disediakan pada: 28-Nov-98 13:39
TA5004 : Decision Programs
You can copy the program in this page and paste it into your editor. Then you can save, compile and run. Save it as the class name dot java.
/* First decision program */
import ccj.*;
public class MyDecision1
{ public static void main(String[] args)
{
System.out.println("5 < 8" + " is " + (5 < 8));
System.out.println("5 > 8" + " is " + (5 > 8));
System.out.println("5 <= 5" + " is " + (5 <= 5));
System.out.println("5 >= 5" + " is " + (5 >= 5));
System.out.println("5 == 8" + " is " + (5 == 8));
System.out.println("5 != 8" + " is " + (5 != 8));
} /* end of main */
} /* end of class */
OUTPUT:
5 < 8 is true
5 > 8 is false
5 <= 5 is true
5 >= 5 is true
5 == 8 is false
5 != 8 is true
/* 2nd decision program -- using if */
import ccj.*;
public class MyDecision2
{ public static void main(String[] args)
{
System.out.print("Type in an integer:");
int a = Console.in.readInt();
System.out.print("Type in another integer:");
int b = Console.in.readInt();
if (a > b)
System.out.println("Your first number is bigger.");
if (b > a)
System.out.println("Your second number is bigger.");
if (a == b)
System.out.println("Both numbers are equal.");
} /* end of main */
} /* end of class */
SAMPLE OUTPUT:
Type in an integer:12
Type in another integer:49
Your second number is bigger.
/* 3rd decision program -- using if/else */
import ccj.*;
public class MyDecision3
{ public static void main(String[] args)
{
System.out.print("Type in an integer:");
int a = Console.in.readInt();
System.out.print("Type in another integer:");
int b = Console.in.readInt();
if (a > b)
System.out.println("Your first number is bigger.");
else if (b > a)
System.out.println("Your second number is bigger.");
else
System.out.println("Both numbers are equal.");
} /* end of main */
} /* end of class */
SAMPLE OUTPUT:
Type in an integer:12
Type in another integer:12
Both numbers are equal.
/* 4th decision program -- comparing Strings */
import ccj.*;
public class MyDecision4
{ public static void main(String[] args)
{
System.out.print("Type in a word:");
String word1 = Console.in.readWord();
System.out.print("Type in another word:");
String word2 = Console.in.readWord();
if (word1.equals(word2))
System.out.println("Both words are perfectly equal.");
else if (word1.toUpperCase().equals(word2.toUpperCase()))
System.out.println("Both words are equal on spelling.");
if (word1.compareTo(word2) == 0)
System.out.println("Both words are perfectly equal.");
else if (word1.compareTo(word2) < 0)
System.out.println("First word is 'smaller'.");
else
System.out.println("Second word is 'smaller'.");
} /* end of main */
} /* end of class */
/* 5th decision program -- Logical operations */
import ccj.*;
public class MyDecision5
{ public static void main(String[] args)
{
System.out.print("Student's mark? >");
double marks = Console.in.readDouble();
System.out.print("Number of absents? >");
double absent = Console.in.readDouble();
if (marks >= 40 && absent <= 10)
System.out.println("Pass.");
else
System.out.println("Fail.");
} /* end of main */
} /* end of class */
/* 6th decision program -- Logical operations with Boolean vars. */
import ccj.*;
public class MyDecision6
{ public static void main(String[] args)
{
System.out.print("Student's mark? >");
double marks = Console.in.readDouble();
System.out.print("Number of absents? >");
double absent = Console.in.readDouble();
boolean pass;
pass = (marks >= 40 && absent <= 10);
if (pass)
System.out.println("Pass.");
else
System.out.println("Fail.");
} /* end of main */
} /* end of class */
/* 7th decision program -- Logical operations with Boolean vars. */
import ccj.*;
public class MyDecision7
{
public static void main(String[] args)
{
System.out.print("Student's mark? >");
double marks = Console.in.readDouble();
System.out.print("Number of absents? >");
double absent = Console.in.readDouble();
boolean marks_OK = (marks >= 40);
boolean attendance_OK = (absent <= 10);
boolean pass = (marks_OK && attendance_OK);
if(pass)
System.out.println("Pass.");
else
if(marks_OK)
System.out.println("Fail because of attendance");
else
System.out.println("Fail because of the mark.");
}/* end of main */
}/* end of class */
-- updated: 02-Dec-98 18:10 --
Updated : 12:35 PM 08-Jul-98
1. After compiling my .java program, I try to run it, but the JDK give me this strange message. Why?
C:\javaccj>javac MyClass.java
C:\javaccj>javac MyClass
javac: invalid argument: MyClass
use: javac [-g][-O][-debug][-depend][-nowarn][-verbose][-classpath
path][-nowrite][-deprecation][-d dir][-J] file.java...
Answer: We must run a java application using "java" command, not "javac". It should have been:
C:\javaccj>java MyClass
2. After I compiled my program, why there is no HTM extension document?
Answer: When we compile a .java file, JDK only produce .class file. The HTM file does not come automatically. We must create it. How to create? Open a new file in the Notepad and type the contents. Here is the typical content of the HTM file that include the class to be displayed by the browser or appletviewer. (you can copy and paste it).
<APPLET CODE="MyClass.class" WIDTH=400 HEIGHT=300 IGNORE=""></APPLET>
Each time you compile, you can view the result of the compilation by viewing the applet (actually viewing the webpage that contains it.). Let's say that the HTM file you created is Mypage.htm, so you can view the applet using this DOS command:
appletviewer Mypage.htm
3. What is the correct way to use readMouse()?
Answer: We must use that method with some 'message' into it, and it will 'give back' the point where the user click. Example:
Point p = readMouse("Click for a point");
Here is a sample program that read a mouse click and draw a point there:
import ccj.*;
public class MyClass extends GraphicsApplet
{ public void run()
{
String message;
message = "Click for point 1";
Point p = readMouse(message);
p.draw();
message = "Click for point 1";
Point q = readMouse(message);
q.draw();
/* add some more here... */
}
}
4. How can we expand the space in the appletviewer so that it could accomodate my long string. For example, I would like to have the message on the appletviewer at coordinate (-9,4) (using Message class). Do we have to split the message into two and set two different coordinates for them?
Answer: You can have the message on one line but it will be cut off to around 40 characters only. I think that's because the class Message was implemented by the author that way. So I think you have to split your long message into two and set two different coordinates for them. To have the message on one line:
String arahan = "This program only works for annual " +
"salary in the range RM60000 and 80000";
To have the message on two lines:
String arahan1 = "This program only works for annual ";
String arahan2 = "salary in the range RM60000 and 80000";
5. How do we indent our codes in so many pairs of "{" and "}"?
Answer: Below is a program that follows our standard. (of course most of the { and } for the if's and else's in this example are unnessessary, but it serve the purpose of showing how to indent the lines with many { and } )
/* Program to show how indentation should be done */
import ccj.*;
public class BiggestFrom2
{
public static void main(String[] args)
{
System.out.print("Type in a integer:");
int a = Console.in.readInt();
System.out.print("Type in another integer:");
int b = Console.in.readInt();
if(a > b)
{
System.out.println("Your first number is bigger.");
}
else
{
if(b > a)
{
System.out.println("Your second number is bigger.");
}
else
{
System.out.println("Both numbers are equal.");
}
}
}/* end of main */
}/* end of class */
6.
Why do I get this error message?
7. Incompatible type for -. Can't convert java.lang.String to int.
8. System.out.println("Overtime pay = "+hour-40+"* "+wage+"*150%" );
^
Answer: If we want to embed straight calculations inside println, put them inside (). Ex:
System.out.println("Overtime pay = " + (hour-40) + "* " + wage + "*150%");
This is ALL. I've give enough help. No more hints after this!
If you really have no time, here are some points to focus:
2. Introduction To Computing -- 10 points,
3. Introduction To Programming -- 10 points,
4. Decisions -- 10 points,
5. Loops -- 10 points,
6. OOP(I, II, III) -- 20 points,
7. Arrays -- 10 points,
8. Data Structures -- 10 points,
9. Algorithm Analysis -- 10 points,
10. Programming Languages -- 10 points,
11. TOTAL: 100 points
Below is the the exam directions, taken from the actual document.
Last updated on 23-Sep-99 13:35.
TA5004 : Lecture 2: Fundamental Data Types
Variables, operators and Strings
You can copy the program in this page and paste it into your editor. Then you can save, compile and run. Save it as the class name dot java.
/* answer to lecture 2 in-class programming exercise1 */
public class Exercise1
{ public static void main(String[] args)
{
double itemPrice = 100.00;
double discRate = .20;
double discounted = itemPrice - itemPrice*discRate;
System.out.println("The discounted price is: " + discounted);
}
}
/* answer to lecture 2 in-class programming exercise2 */
import ccj.*;
public class Exercise2
{ public static void main(String[] args)
{
System.out.print("Type in the item price: >");
double itemPrice = Console.in.readDouble();
System.out.print("Type in the item discount rate: >");
double discRate = Console.in.readDouble();
double discounted = itemPrice - itemPrice*discRate;
System.out.println("The discounted price is: " + discounted);
}
}
/* program to demo the +=, -=, ++ and -- */
public class OperatorsDemo
{ public static void main(String[] args)
{
int a = 10, b = 5, c = 3;
System.out.println("Value of a : " + a);
System.out.println("Value of b : " + b);
System.out.println("Value of c : " + c);
a += b;
System.out.println("Value of a after a += b : " + a);
a -= c;
System.out.println("Value of a after a -= c : " + a);
a++;
System.out.println("Value of a after ++ : " + a);
a--;
System.out.println("Value of a after -- : " + a);
}
}
/* answer to lecture 2 in-class programming exercise3 */
import ccj.*;
public class Exercise3
{ public static void main(String[] args)
{
System.out.print("Type in the first integer : >");
int number1 = Console.in.readInt();
System.out.print("Type in the second integer : >");
int number2 = Console.in.readInt();
System.out.print("Type in the third integer : >");
int number3 = Console.in.readInt();
int total = number1 + number2 + number3;
System.out.println("The total of " + number1 + ", " + number2
+ ", " + number3 + " is " + total);
}
}
/* Sample programming using String in Java */
import ccj.*;
public class String1
{ public static void main(String[] args)
{
String name = "Zamberi";
name = name + " Saad";
System.out.println("My name is " + name);
System.out.println("Please enter your name: > ");
name = Console.in.readLine();
System.out.println("Your name is " + name);
int n = name.length();
System.out.println("Your name length is " + n);
} /* end of main */
} /* end of class */
n
updated: 22-Nov-98 10:20 –