Wednesday, 24 October 2018

LAB 6
Choong Kar Yee
182630
1. 
public class Q1 {
public static void main(String[] args) {

System.out.printf( "%6b", ( 1 > 2 ) );
System.out.printf( "%6s\n", "Java");
System.out.printf("Amount is %f %e", 32.32, 32.32);
System.out.printf("%8d%8.1f", 1234, 5.6321);
System.out.printf("%-8d%-8.1f", 1234, 5.6321);

}
}



2.

public class Q2 {
public static void main(String[] args) {

String s1 = "Welcome to Java";
String s2 = "Welcome to Java";
String s3 = new String("Welcome to Java");
System.out.println( s1 == s2);
System.out.println( s1 == s3);

}
}



3.

import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("Enter 2 name,sister1:");
String sister1= input.nextLine();
System.out.print("sister2:");
String sister2= input.nextLine();

if (sister1.compareTo(sister2)<0)
{
System.out.println("sister1 is smaller than sister2");
}
else if (sister1.compareTo(sister2)>0)
{
System.out.println("sister1 is greater than sister2");
}
else
{
System.out.println("They are same age!");
}
}
}


4.

public class Q4 {
public static void main(String[] args) {
int number=(int)(Math.random()* 10 +'a');
System.out.println((char)(number));

}
}



5.
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("Enter a character:");
char E =input.next().charAt(0);
int ASCII =(int)E;

System.out.println("The ASCII code for character" + E + "is" + ASCII);

}
}


6.

import java.util.Scanner;
public class Q61 {
public static final int PASSWORD_LENGTH = 8;
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("A password must have at least eight characters.\n" +
"A password consists of only letters and digits.\n" +
"A password must contain at least two digits.");
System.out.print("Enterpassword");
String password= input.nextLine();

        if (isValid(password)) {
            System.out.println("Valid Password");
        } else {
            System.out.println("Invalid Password");
        }
    }
    public static boolean isValid(String password) {
        if (password.length() < 8) { 
            return false;
        } else {    
            char c;
            int count = 1; 
            for (int i = 0; i < password.length() - 1; i++) {
                c = password.charAt(i);
                if (!Character.isLetterOrDigit(c)) {        
                    return false;
                } else if (Character.isDigit(c)) {
                    count++;
                    if (count < 2)   {   
                        return false;
                    }   
                }
            }
        }
        return true;
    }
}
LAB 5
Choong Kar Yee
182630

SWITCH STATEMENT
Tell you secretly
This is my favourite topic among others! 😌


Let's begin!

1.
a. ( true ) && ( 3 > 4 ) – FALSE
b. ! ( x > 0 ) && ( x > 0 ) – FALSE
c. ( x > 0 ) || ( x < 0 ) – TRUE 
d. ( x != 0 ) || ( x = = 0 ) – TRUE
e. ( x >= 0 ) || ( x < 0 ) – TRUE
 f. ( x != 1 ) = = !( x = = 1 )- TRUE

2.
public class no2
public static void main(String[] args) {
int x=1, a=3;
switch (a) {
case 1: System.out.println (x+=5);
break;
case 2: System.out.println (x+=10);
break;
case 3: System.out.println (x+=16);
break;
case 4: System.out.println (x+=34);
}



3.
y =2
x=3;
y=3;
if (x+3==6)
{
y=1;
y+=1

4.
public class Day 
public static void main(String[] args) {
swtich (day){
case 0:System.out.println("Sunday");
break;
case 1:System.out.println("Monday");
break;
case 2:System.out.println("Tueday");
break;
case 3:System.out.println("Wednesday");
break;
case 4:System.out.println("Thursday");
break;
case 5:System.out.println("Friday");
break;
case 6:System.out.println("Saturday");
}
}

5.
public class Day 
public static void main(String[] args) {
swtich (month){
case 1:System.out.println("January");
break;
case 2:System.out.println("February");
break;
case 3:System.out.println("March");
break;
case 4:System.out.println("April");
break;
case 5:System.out.println("May");
break;
case 6:System.out.println("June");
break;
case 7:System.out.println("July");
break;
case 8:System.out.println("August");
break;
case 9:System.out.println("September");
break;
case 10:System.out.println("October");
break;
case 11:System.out.println("November");
break;
case 12:System.out.println("December");

}
}

6.
((age>=6)?"ticketPrice=20":"ticketPrice=10");


7.
a. 
score = ( x > 10 ) ? 3 * scale : 4 * scale;

if (x>10)
score= 3*scale;
else
score=4*scale;

b.
tax = ( income > 10000 ) ? income * 0.2 : income * 0.17 + 1000;


if(income>10000)
tax=income* 0.2;
else
tax=income*0.17+1000;


c.
System.out.println( ( number % 3 == 0 ) ? i : j );

if ( number % 3 == 0 )
System.out.printIn(i);
else
System.out.printIn(j);


8.
import java.util.Scanner;
public class SortNumber {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter three numbers:");
int w = input.nextInt();
int x = input.nextInt();
int y = input.nextInt();

if (w<x)
{
if (x<y)
{
System.out.println("The numbers are sorted.");
}
else
{
System.out.println("The number are not sorted");
}
}
else
{
System.out.println("The numbers are not sorted.");
}
}
}



b.
import java.util.Scanner;
public class SortNumber1 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter three numbers:");
int w = input.nextInt();
int x = input.nextInt();
int y = input.nextInt();

if ((w<x)&&(x<y))
{
System.out.println("The numbers are sorted.");
}
else
{
System.out.println("The numbers are not sorted.");
}
}
}


9.
import java.util.Scanner;
public class Tax {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter filling status:0 for single filers, 1 for married filling jointly or qualified widow(er), 2 for married filing separately, 3 for head of household.");
int filingStatus=input.nextInt();
System.out.print("Enter taxableIncome");
int taxableIncome=input.nextInt();


double tax=0.0;
switch (filingStatus){
case 0:
if (taxableIncome>82250)
System.out.println(tax=(0.10*8350)+0.30*(taxableIncome-82250));
if (taxableIncome>33950)
System.out.println(tax=(0.10*8350)+0.25*(taxableIncome-33950));
if (taxableIncome>8350)
System.out.println(tax=(0.10*8350)+0.15*(taxableIncome-8350));
else
System.out.println(tax=0.10*taxableIncome);
break;

case 1:
if (taxableIncome>137050)
System.out.println(tax=(0.10*16700)+0.30*(taxableIncome-137050));
if (taxableIncome>67900)
System.out.println(tax=(0.10*16700)+0.25*(taxableIncome-67900));
if (taxableIncome>16700)
System.out.println(tax=(0.10*16700)+0.15*(taxableIncome-16700));
else
System.out.println(tax=0.10*taxableIncome);
break;

case 2:
if (taxableIncome>68525)
System.out.println(tax=(0.10*68525)+0.30*(taxableIncome-68525));
if (taxableIncome>67900)
System.out.println(tax=(0.10*33950)+0.25*(taxableIncome-33950));
if (taxableIncome>16700)
System.out.println(tax=(0.10*8350)+0.15*(taxableIncome-8350));
else
System.out.println(tax=0.10*taxableIncome);
break;

case 3:
if (taxableIncome>117450)
System.out.println(tax=(taxableIncome-117450)*0.30);
if (taxableIncome>45500)
System.out.println(tax=(taxableIncome-45500)*0.25);
if (taxableIncome>11950)
System.out.println(tax=(taxableIncome-11950)*0.15);
else
System.out.println(tax=taxableIncome*0.10);
break;
default: System.out.println("Error: Invalid status");
}
System.out.println("Total tax is " + tax);
}
}


Wednesday, 17 October 2018

LAB 4
Choong Kar Yee
182630


Yo! What's up!
Today we were given a task to create our OWN game.
Sounds interesting ha?!
Alright
This is the time to test how far we have learnt in the class (Tricky)
i have created my own game to save a QUEEN.
Let's see how it works!



Unfortunately
in the halfway
i have found out alot of errors
AND
FORTUNATELY
I have the BEST LECTURER in the house!
He helped me found out my mistake
which is
OMG
i just missed out one bracket
JUST ONE BRACKET
and my entire program went fatal.

THANKS GOD
and
THANKS DR.
for helping.
Appreciate aLOT
and
SALUTE

Wednesday, 10 October 2018


Choong Kar Yee
182630
Lab3

1.


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);
              }

}


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);
}
}


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.





import java.util.Scanner;
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

lab 10 1. import java.util.Scanner; public class Test1{      public static void main(String []args){               Scanner input =...