LAB 6 - Introduction to Programming (part 1)

Lab Exercises

Topics

Introduction to Programming

Class and Object Methods

 

Exercises

 

3.3 Class and Object Methods

 

What is the value printed at each step?

            String str1 = “Hello World”;

            System.out.println(str1.length());

            System.out.println(str1.toLowerCase());

            System.out.println(str1.toUpperCase());

 

            str1 = “Goodbye World”;

            char ch = str1.charAt(5);

            System.out.println(ch);

 

            str1 = “Computer” + “Science”;

            System.out.println(str1);

 

            str1 = str1.concat(“ is fun”);

            System.out.println(str1);

           

            str1 = "smiles".substring(1, 5);

            System.out.println(str1);

 

Try typing the statements into the interaction pane to check your answers.

A complete list of String methods can be found at the API (http://java.sun.com/j2se/1.5.0/docs/api/index.html)

 

What is the value printed at each step?

            int nbr1 = -37;

            double nbr2 = 23.45;

           

            System.out.println(Math.abs(nbr1));

 

            System.out.println(Math.sqrt(nbr2));

 

            System.out.println(Math.ceil(nbr2));

 

            System.out.println(Math.sin(nbr1));

 

            System.out.println(Math.min(nbr1, nbr2));

 

            System.out.println(Math.round(nbr2));

 

Try typing the statements into the interaction pane to check your answers.

A complete list of Math methods can be found at the API (http://java.sun.com/j2se/1.5.0/docs/api/index.html)

 

What is the value printed at each step?

 

            System.out.println(Integer.MAX_VALUE);

 

            System.out.println(Integer.MIN_VALUE);

 

            System.out.println(Integer.toHexString(250));

 

            System.out.println(Integer.toBinaryString(250));

 

            System.out.println(Character.isDigit(‘a’));

 

Try typing the statements into the interaction pane to check your answers.

A complete list of Integer, Character, Boolean, Byte, Double, Float, Long, and Short methods can be found at the API (http://java.sun.com/j2se/1.5.0/docs/api/index.html)

 

Copy the following program into the definition pane, compile and execute.

 

//********************************************************************

//  RandomNumbers.java       Author: Lewis/Loftus

//

//  Demonstrates the creation of pseudo-random numbers using the

//  Random class.

//********************************************************************

 

import java.util.Random;

 

public class RandomNumbers

{

   //-----------------------------------------------------------------

   //  Generates random numbers in various ranges.

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

      Random generator = new Random();

      int num1;

      float num2;

 

      num1 = generator.nextInt();

      System.out.println ("A random integer: " + num1);

 

      num1 = generator.nextInt(10);

      System.out.println ("From 0 to 9: " + num1);

 

      num1 = generator.nextInt(10) + 1;

      System.out.println ("From 1 to 10: " + num1);

 

      num1 = generator.nextInt(15) + 20;

      System.out.println ("From 20 to 34: " + num1);

 

      num1 = generator.nextInt(20) - 10;

      System.out.println ("From -10 to 9: " + num1);

 

      num2 = generator.nextFloat();

      System.out.println ("A random float (between 0-1): " + num2);

 

      num2 = generator.nextFloat() * 6;  // 0.0 to 5.999999

      num1 = (int)num2 + 1;

      System.out.println ("From 1 to 6: " + num1);

   }

}

 

A complete list of Random  methods can be found at the API (http://java.sun.com/j2se/1.5.0/docs/api/index.html)

                       

QUESTIONS:

  1. Write a statement that prints the number of characters in a String object called overview.
  2. Write a declaration for a String variable called change and initialize it to the same characters stored in another String called original with all ‘e’ characters changed to ‘j’.
  3. Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3.
  4. Write code to declare and instantiate an object of the Random class and use nextInt to generate a random number between 1 and 6.
  5. What are the MAX_VALUE for Byte and MIN_VALUE for Short?

 

Submit the answers through the DropBox in WebCT.