Choong Kar Yee
182630
Lab3
1.
public static void main(String[] args){
double a = 6.5;
a += 1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
a += a + 2;
System.out.println(a);
}
}
Line
|
a
|
output
|
3
|
6.5
|
|
4
|
7.5
|
|
5
|
|
7.5
|
6
|
6.0
|
|
7
|
3.0
|
|
8
|
|
3.0
|
9
|
8.0
|
|
10
|
|
8.0
|
public class Test2 {
public static void main(String[] args) {
int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
a = 6;
b = --a;
System.out.println(a) ;
System.out.println(b);
//a = 6, b = 8, ” a + b” is 14.
}
}
Line
|
a
|
b
|
output
|
3
|
6
|
|
|
4
|
|
7
|
|
5
|
|
|
6
|
6
|
|
|
7
|
7
|
6
|
|
|
8
|
|
5
|
|
9
|
|
|
6
|
10
|
|
|
5
|
Import java.util.Scanner;
public class EvenOrOddNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
if (number % 2 == 0)
System.out.println(number + " is even.");
System.out.println(number + " is odd.");
}
}
line
|
number
|
remainder
|
output
|
5
|
110
|
|
|
6
|
|
0
|
|
7
|
|
|
110 is even.
|
|
|
|
|
5
|
105
|
|
|
6
|
|
1
|
|
10
|
|
|
105 is odd.
|
2.
public class Test22 {
public static void main(String[] args) {
int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
a = 6;
b = --a;
System.out.println(a) ;
System.out.println(b);
}
}
3.
public class Test1 {
public static void main(String[] args){
double a = 6.5;
a += 1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
a += a + 2;
System.out.println(a);
}
}
public static void main(String[] args){
double a = 6.5;
a += 1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
a += a + 2;
System.out.println(a);
}
}
4.

5.
public class Test3 {
public static void main(String[] args) {
boolean cheeseIsYellow = true;
boolean moonIsYellow = true;
if (cheeseIsYellow)
if (moonIsYellow)
{
System.out.println("Moon is made of cheese");
}
}
}
5.
public class EnterALetter {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter a Letter");
char c = input.next().charAt(0);
char letter = c;
char d = c;
char before = --c;
char after = ++d;
System.out.println("The letter is" + letter + "It comes before " + (after) + "and after" + (before));
}
}
6.
import java.util.Scanner;
public class Distance{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter x1 and y1");
double x1= input.nextDouble();
double y1= input.nextDouble();
System.out.println("Enter x2 and y2");
double x2= input.nextDouble();
double y2= input.nextDouble();
double distance=Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2));
System.out.println("The distance between two point is" + distance);
}
}
7.
import java.util.Scanner;
public class ExchangeRate{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
final double rate=0.2414;
System.out.println("Enter 0 to convert RM to USD and 1 vice versa");
int convert= input.nextInt();
if (convert==0)
{
System.out.println("Enter the RM amount");
double RM=input.nextDouble();
double USD=RM*rate;
System.out.println("RM" + RM + "is USD" + USD);
}
else if (convert==1)
{
System.out.println("Enter the USD amount");
double USD=input.nextDouble();
double RM=USD/rate;
System.out.println("USD" +USD + "is RM" + RM);
}
else
{
System.out.println("incorrect input");
}
}
}
8.
import java.util.Scanner;
public class RockPaperScissorGame{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
{
double min=0.0;
double max=1.0;
double computer=min+(int)(Math.random()*(max-min)+1);
System.out.println("Rock(0),Paper(1),Scissor(2):");
}
{
double user=input.nextDouble();
System.out.println("The computer play scissor: You are paper.You lose.");
}
{
double user=input.nextDouble();
System.out.println("The computer plays scissor: You are scissor too. It is a draw.");
}}
}
Huhhhh!
Some questions were really hard tho!..
But i had try my best to figure out solutions for every question. Some questions are a bit TRICKY and COMPLICATED!
Somehow, i manage to solve it with my gang of friends!
YEAH!!
LAB 3 DONE!
BYeeeeeee
No comments:
Post a Comment