More Related Content
Similar to Mgd01 lab01to03
Similar to Mgd01 lab01to03 (20)
More from Hock Leng PUAH (20)
Mgd01 lab01to03
- 1. &¨ )( © ¤¢('¨''¨%$ ¥§#
)¥I H¥† ’ ¥¤£@
%“ £§)£¤§ ¥¤ © ) ’‘ £§)£¤§ §§ H¥ § ¥§¥E %C ¥…
‰(ˆ(‡ ©§¥¢¥† §… %7 ¥…
„yrugƒr‚wwv gg€ syx wwv utsrq p` idc hgfedcba`
4UQYU2QX U2WVVQTU1TS QRQP
©¥ §§I!'¥(©H$($§¨%$
%¥)¥¥B¥G
© '§ © ££' E %F6 ¢§ 876¦¡ o
© £©¥ E '§ ©¥9D §§ %C6 ¢§ 876¦¡ o
£§)£¤A §§ B A ©£@ 9¡ %76 ¢§ 876¦¡ o
¥§!§ £¨¨§ §§ •
53243210
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 2. ¨ )( © ¤¢('¨''¨%$ ¥§#(
§
£'¨S §
§£§†S §
©)¥I S ¦¥¤£¢¡ )
A§§©£@ %¥¨§ )¥I
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 3. ¨ )( © ¤¢('¨''¨%$ ¥§#2
“‡@ )¥I £§¡ !G ’ !G %!G '§ '¤£! %D ¥…
}
}
System.out.println(My First Java App);
public static void main(String[] args) {
public class Main {
package firstjavaapp;
§§I(£§¡
%“'¥ £ ©¥' ) £H ¤¤ B ¥$ ''A %F ¥…
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 4. ¨ )( © ¤¢('¨''¨%$ ¥§#3
¡ ¥¤££! @¡£¥@ ¡ ¡ ¡ ¦¥ 8
X“¥£ ¥£(¥¤ © E(¨¥©9…X ¨E §¤£¨£© ©£ X“¤£(! (¨¥©9…X “D
£§)£¤§ ¥$ B £ £§© ¥$ ©£ X“©£§¨ '£ )£§© )£¤¢!X “F
X§§§I©£BX '¥¤¤§) ¥'¤ B § ¥'£©£ '¥)§¤ ©£ ©©§¤) ¥$ ¥£§¨£ ’‘ X§§§I©£B ¥§¦)§X
(£$©§¤) ¥¨§
'£ § ©£ ¥© ! (¥)§©¥¨§ ¨E §¤£¨£© ©£ § (¥' ) B ¥£¤ ©£B ¥$ ¥¢ ©!¨ ' H9¥¦ X¥§¦)§X “C
¡ ¥ ©©§¤) )£¤¢! '§ §§I(¡ ¥
¥¨§© ¥$ ¥¢ ©!¨ ¥¨§ £¥$ '§ ©©§¤) )£¤¢! ¥ £§ ) 9¤ §) ¥¤£B §§ 9¥¨ “7
}
}
System.out.println(My First Java App);
public static void main(String[] args) {
public class Main {
package firstjavaapp;
§§I(£§¡
¤¤£¡£ £¡ ¢¢¢¡ ¢¤ £¢ ¡ ))
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 5. ¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
t„yrts¡„y£ ¢„g t¡wp gugb i©c hgfedcba`
‰(ˆ(‡ ©§¥¢¥† §… %7 ¥…
©¥9D§§'A©¥¤¢§£§D %¥¨§ )¥I ( £§)£¤§ §§ H¥ § ¥§¥E %C ¥…
%“¥!¤¢ £ ©¥' ) £H ¤¤ B ¥$ ''A %F ¥…
§§I(£§¡
package variablesanddatatypes;
public class Main {
public static void main(String[] args) {
// Variable: must begin with a letter or underscore and
// must consist entirely of letters, digits, and underscores.
/* Data types
- Primitives: byte, short, int, long, float, double,
char and boolean
- Objects: String
*/
double capital = 200.0;
double interestRate = 0.01;
double subTotal = capital * interestRate;
System.out.println(SubTotal = + Double.toString(subTotal));
// Using String
String input = 35;
double total = subTotal + Double.parseDouble(input);
System.out.println(Total = + Double.toString(total));
// Using Math utility class
int length = 5;
double areaOfSquare = Math.pow(length, 2);
System.out.println(Area of Square = + areaOfSquare );
}
}
¥§#B ¨ )( © ¤¢('¨''¨%$
- 6. ¨ )( © ¤¢('¨''¨%$ ¥§#D
““©£… D ¥ %¥©§)
¥! $£H §© 9¥$ ¥¨E @ “©£… ¥ %¥©§) ¥H ¤ $£H §© ©' $¥¨ ©©§¤) B £¥ ) £¨§† “I
“©£… D( ¥¦£¤
©' $¥¨ H £¥$ ¥§$ 9¥$ '§ ©)¥I¢ ¥§ D¨†( £ ¥9 §§' ¤¤§ ©§ ¨E ¨ B ¥¥BB£
“©¥¤¢! ¥©§(¥¤¢! '§ “©£… (¥¤¢!
%£©! £©¥ ) ¥9 §§ “D
£… ©£ ¥9 )¥I¢ i
§¥¤ ¢ '§ §$) ¥¥¤¢! ' ¥§ ¤B ¥ ¤ ¥£ ¥ $© ¥¥9¢ ¥§ ©¥9 ¥££¨£ i
%©¥9 §§ “F
C¥¨§†89¨ ¥¥¨§†9¡ ¥¥¤¢§£§D¥§£8 %'£¤§ ¤¤§ ¥§ £H ¤¤ B ¥$D (©¥ )©¥'!
'§ ¥©££' ¥©¥¥¤ B 9¤¥£¥ ©£© ) ©!¨ '§ ¥ )©¥'! ¥¥¤ § $£H £¥¢ ©!¨ %©¥¤¢§£§ B ¥¨§† “C
©¥¨¨ ) ¦) ¤¢ B ¡ (© ¡ G ©¥¨¨ ) ¥£¤ ¥¤£© B ¥©e %©¥¨¨ E “7
¤¤£¡£ £¡ ¢¢¢¡ ¢¤ £¢ ¡ ))
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 7. ¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
twyyf ¢„g t„yrur¢„y£ i2c hgfedcba`
‰(ˆ(‡ ©§¥¢¥† §… %7 ¥…
© 'A£$)§ %¥¨§ )¥I ( £§)£¤§ §§ H¥ § ¥§¥E %C ¥…
%“¥!¤¢ £ ©¥' ) £H ¤¤ B ¥$ ''A %F ¥…
package conditionsandloops;
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
public class Main {
public static void main(String[] args) {
// do while loop
System.out.println(--do while loop--);
boolean wantsToContinue = true; // True to loop again.
int loopcount = 0;
do {
loopcount++; // add 1 to loopcount
if (loopcount == 10)
wantsToContinue = false; // check to set variable
System.out.println(Loop: + loopcount);
} while (wantsToContinue == true); // check at the end
System.out.println(After do while loop);
// while loop to calculate
System.out.println(--while loop--);
int years = 0; // initialize the variable years
double interest, principal, rate;
principal = 1000.0;
rate = 0.01;
while (years 5) { // condition for continuing loop
interest = principal * rate; //
principal += interest; // do three statements
System.out.println(Year + (years+1) + : + principal);
years++; // update the value of the variable, years
}
// for loop, doing same thing as the above while loop
System.out.println(--for loop--);
principal = 1000.0;
for (years = 0; years 5; years++) {
interest = principal * rate;
principal += interest;
System.out.println(Year + (years+1) + : + principal);
}
// for each and enum Day
¥§#E ¨ )( © ¤¢('¨''¨%$
- 8. ¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
// enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
System.out.println(for each loop);
for (Day d : Day.values()) {
System.out.print(d); // print the day
System.out.print( is day number );
System.out.println(d.ordinal()); // print the position
}
// if else
System.out.println(--if else--);
int myAge = 15;
if (myAge 18) { // Check condition for above 18 yrs old
System.out.println(Under the Road Traffic Act,you can drive.);
}
else { // otherwise, below 18 yrs old
System.out.println(Under the Road Traffic Act,you cannot drive.);
}
// switch and enum Day
System.out.println(--switch--);
Day day = Day.WEDNESDAY;
switch (day)
{
case MONDAY: System.out.println(Mon);
break;
case TUESDAY: System.out.println(Tue);
break;
case WEDNESDAY: System.out.println(Wed);
case THURSDAY: System.out.println(Thu);
case FRIDAY:
System.out.println(Fri);
break;
default:
System.out.println(Default);
break;
}
}
}
¥§#F ¨ )( © ¤¢('¨''¨%$
- 9. ¨ )( © ¤¢('¨''¨%$ ¥§#G
}
}
} while (exitLoop.equalsIgnoreCase(N));
exitLoop = scanner.nextLine();
System.out.print(Exit loop (Y/N):);
do {
String exitLoop;
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
public class Main {
¤¥§)…(¤£!(§§I ¨£ %£s
(¨§ ¥$ £‰¥ ©¥© $) ¥©! ¥$ ¤£! $! $
¤ ¨§ ¥ ¢§ ¥$ 9B£' ¨ ¥ ¤ ¥¤£$H ' '§ “!£ ¥©! '§¥ ©©§¤) ¥§)… ¥$ £©e ¦( ¡ P
Your total roll is 7
The second die comes up 4
The first die comes up 3
%¥¤¨§‰¥ @ (¤¤ ¤§ ¥$ '§ ¥£' $)§¥ £H $© ¥¢¨!
¥$ ¥ '¤! $© ¨§ ! (¤¤ ¤§ ¥$ ¥ ¥$¥ ¥!¤§ £¥$ ''§ '§ ¥£' ' )¥© ¥$ B £§§ £
+ 1 int rolledNumber1 = (int) (Math.random() * 6)
7¥¢¨!†'¥¤¤ ¥¤¢§£§ ¥$ £©©§ '§ ‡ 7 ¨ B ¥¢¨! § ¥§¥¥ ¤¤£H ¥¨¥§© £H ¤¤ B ¥$D %£s
('¥¤¤ ©£ £ ¥B§ ¥£' ¥$ ¥¢¨! ¥$ ©¥©¥¥ ¦)£ ! 9 ¥¢¨! ¥$D (¨ '§ § ‡ ¥I ¥D ¥F ¥C ¥7 ©¥¥£ ¥$ B
¥ £© $) 9¢ ¥£' ¥ £¤¤ ¥§¤!¨£© §) ! (¥)£' B £§ § B £¤¤ ¥§¤!¨£© §$ ¨§ § ¥£ ¦ ¡ P
¡trƒs¡ H
¤ ‰¥ ¥$ ¨!I %¥!£ )
¤ ¥$ B ! ¨!I %¦§¥¢
%¥!£ ) '§ ¦§¥¢ “F
“I!$ ¤¤§B ¦§¤¢ 9¤ ¨) ¨ B ¥¥BB£' $! $ ¤¤§B ©H ¤¤§ $)£H© $)£H© '§ ¥©¤¥ B£ %£$)§ “C
“¨) ¨ B ¥¥BB£' ©£ ‰§9© $)§¥ B $)§¥ B '§ B ¥¥¤£$H ¥¥¤£$H ' %© “7
¤¤£¡£ £¡ ¢¢¢¡ ¢¤ £¢ ¡ ))
¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
- 10. ¥§!§ £¨¨§ §§ ¥¨ ¤¥¥ ©¥¨§¦ ¥¤£¢ ¡
The first die comes up 3
The second die comes up 4
Your total roll is 7
Exit loop (Y/N):N
The first die comes up 3
The second die comes up 5
Your total roll is 8
Exit loop (Y/N):
¥ § # ¡ ¨ )( © ¤¢('¨''¨%$