STIA1123 Exercise 2 - Predefined Class

1.       Write an application that calculates the squares and cubes of the numbers from 1 to 10 and prints the resulting values in a table format (use printf()).  You must use Math class to solve this problem.

 

            Sample run: 

 

Number        Square         Cube

------------------------------------

1              1              1        

2              4              8        

3              9              27       

4              16             64       

5              25             125      

6              36             216      

7              49             343      

8              64             512      

9              81             729      

10             100            1000


public class MathCalc {


   public static void main (String[] args) {

   


   System.out.println ("Number     Square     Cube");

   System.out.println ("---------------------------");

   

     for (int i=0; i<11; i++){

   System.out.printf (i + "            ");

   System.out.printf (Math.pow(i,2) + "           ");

   System.out.printf (Math.pow(i,3) + "    ");   

      System.out.println ();

   }


   

   }

  }   


2.      Write a program called CountA that accepts a String input and display the number of times the character 'A' or  'a' is found in the string.

 

Sample output:

 

Universiti Utara Malaysia, College of Arts and Sciences

Total of A is 1

Total of a is 6


import java.util.Scanner;
public class CountA {
  public static void main(String[] args) {
     
        String sentence;
        int count = 0;
        int count1 = 0;
     
        System.out.println ("Please enter your string");
        Scanner scan = new Scanner(System.in);
        sentence = scan.nextLine();
       
        for (int i = 0 ; i < sentence.length(); i++ ){
            if (sentence.charAt(i)== 'A'){
            count++;
            }
           
            else
            if (sentence.charAt(i)== 'a'){
            count1++;
            }
        }
        System.out.println ("Total of A is " + count);
        System.out.println ("Total of a is " + count1);
  }
       
       
   
         
}


3.      Write a program called ReverseString that accepts a String input and print the  string that contains the characters of the String in reverse order.

 

Sample run:

Before Reverse:

Universiti Utara Malaysia, College of Arts and Sciences

After Reverse:

secneicS dna strA fo egelloC ,aisyalaM aratU itisrevinU


import java.util.Scanner;
public class ReverseString {
    public static void main(String[] args) {
     
        String sentence;
        String nsentence = "";
        char character;
        System.out.println ("Before Reverse:");
        Scanner scan = new Scanner(System.in);
        sentence = scan.nextLine();
       
        for (int i=0; i<sentence.length(); i++){
        character = sentence.charAt(i);
        nsentence = character + nsentence;
        }
        System.out.println("After Reverse:");
        System.out.println(nsentence);
}
}


4.      Write a program called RandomIntInRange that accepts two integers representing a range. Display five random integers in the specified range (inclusive). Display zero if the first integer is greater than the second integer. You must use Random class to solve this problem.

 

Sample run:

Enter two integers representing a range:

1 10

Random number 5

Random number 10

Random number 5

Random number 3

Random number 1


import java.util.Scanner;

import java.util.Random;


public class RandomIntInRange {

    public static void main(String[] args) {

        

        int int1;

        int int2;

        

        System.out.println ("Enter two integers representing a range:");

        Scanner scan = new Scanner(System.in);

        Random random =new Random();

        int1 = scan.nextInt();

        int2 = scan.nextInt();

        

        if (int1 > int2){

        System.out.println("0");

        }

        else for (int i=0; i<5; i++){

            int randomInt = random.nextInt(int1,int2+1);

            System.out.println("Random number "+randomInt + " " );

        

            if (randomInt<int1){

            

            }

        }


}

}




Comments

Popular posts from this blog

Exercise 3 - PredefinedClass

Exercise - User definedClass-2

Exercise - User definedClass-3