Exercise 3 Predefined Class

1.                Write TWO (2) different ways to create an object from the String class.

 

public class Main {

 

public static void main (String[] args {

  String object1 = "Najihah Najmy";

  String object2 = new String("Najihah Najmy");

  }

}

 

      2.            What is the output for the following Java statements?


String phr = new String("Big,blue sky");

System.out.println(phr.substring(phr.indexOf(","),7));

 

public class Question2{

    public static void main(String[] args) {

       String phr = "Big,blue sky";

System.out.println(phr.substring(phr.indexOf(","),7));

    }   

}



 

     3.            Give your description on the output that will be produced by the execution of the following statements.

Random rd = new Random();

System.out.println(rd.nextInt(41) + 10);

 

 

import java.util.Random;

    public static void main(String[] args) {

       Random rd = new Random();

       System.out.println(rd.nextInt(41) + 10);

 

}

}



 

    4.            Write Java code segment that use class Random to generate two random numbers that is greater than or equal to 0, and less than 100 and store the values into local variable num1 and num2.  Then use class Math to determine and display the largest between both values.

       import java.util.Random;

public class JavaApplication9 {

 

    public static void main(String[] args) {

        Random num = new Random();

       

        int num1 = num.next(100);

        int num2 = num.nextInt(101);

        System.out.println("the random number 0 to 100: ");

        System.out.println("number1: " + num1 + "\number 2: " + num2);

        System.out.println("the biggest number between this random number is: ");

        System.out.println(Math.max(num1, num2));

    }

   

}

 




    5.            Determine the output of the following code segments:

String test = "abc";

test = test + test;

System.out.println(test);

 

public class JavaApplication13 {

    public static void main(String[] args) {

        String test = "abc";

test = test + test;

System.out.println(test); 

 

    }

    

}



 

 

Random random = new Random();

double d = Math.round ( 2.5 + random.nextInt(1) );

System.out.println("The value is " + d);

 import java.util.Random;

public class JavaApplication10 {


    public static void main(String[] args) {


        Random random = new Random();


double d = Math.round ( 2.5 + random.nextInt(1) );


System.out.println("The value is " + d);

    }    


 

    6.            Class Scanner in Java is used to read input from the console.  Answer all of the following questions:

a) Write a Java statement to instantiate an object input of class Scanner.

 import java.util.Scanner;

public class question6{

public static void main (String args []){

Scanner input = new Scanner(System.in);

 

b) By using the object input, read two integer values and assigned to variable x and y.

system.out.printin("please enter an integer value for x: ");

int x = scan.nextInt () ;

System.out.printIn("x:  ” + x);

system.out.println("please enter an integer value for Y: ");

int y = scan.nextInt();

System, out.println("y:  + y) ;

 

c) By using an appropriate method from class Math, display the value of Xy.

system.out.printin("y: "+ y)i

System. out.print ("x power of y is: “);

System.out.print (Math.pow(x, y)) ;

    }

}

 

    7.            Given below are the statements that declare and create two String objects

String text1 = "School of Computing";

String text2 = "College of Arts and Sciences";

String text3 = "UUM Sintok";

 

a.                Write a Java statement to display the total length of the text1, text2 and text3.

System.out.println(text1.length +text2.length +text3.length());

 

b.               By using the variables of text1 and text2. Write a Java statement to display as output "College of Arts and Computing".


System.out.println(text2.substring(0,20)+text1.substring(10);


Comments

Popular posts from this blog

Exercise UML