Tutorial 3

 

1.Given the following declarations, indicate the value that is assigned in each assignment statement.  That is show what is stored in the iResult, fResult and sResult variables after each assignment. Show each floating point value to three places past the decimal point.

 

            int iResult, num1 = 25, num2 = 40, num3 = 17, num4  = 5, num5 = -14, num6 = -27;

           double fResult, val1 = 17.0, val2 = 12.78;

            String sResult, title = "Java Software Solutions";

 

To help, below is a small list (although not complete) of common Math and String class methods:

Math Class

Math.sin(0)                           //returns 0.0 which is a double - also cos and tan

Math.round(6.6)                 //returns 7 (rounds up and down)

Math.round(6.3)                 //returns 6

Math.ceil (9.2)                      //returns 10 (rounds up always)

Math.floor(9.2)                     //returns 9 (rounds down always)

Math.max(560, 289)                //returns max value 560 - also min Math.min(560, 289) = 289

Math.sqrt(144)                     //returns 12.0 - square root which is a double

Math.pow(5,2)                     //returns 25.0 which is a double (5 to the power of 2)

Math.exp(2)                         //returns 7.38905609893065

Math.log(7.38905609893065)                 //returns 2

Math.random()                   //returns a double >=0.0 and <1.0

 

                String Class

Assume following declarations for examples: String name = "Mark", name2 = "Mary";

 

name.length();                      //returns 4 (the length of Mark)

name.charAt(2);                  //returns r (the first character has index of 0)

name.substring(1,4);                //returns ark (the substring starting at char 1 to char 4)

name.concat(name2);                //returns MarkMary (concatenates the two strings)

name.replace('a', 'x');                //returns Mxrk (replaces all occurrences of 'a' with 'x')

name.toLowerCase();                //returns mark (all lower case letters)

name.toUpperCase();                //returns MARK (all upper case letters)

 

            a)  iResult = title.length();                              b)  iResult = title.indexOf('t');

                 iResult =                                                                iResult =

 

 

 

            c)  sResult = title.toUpperCase();                 d)  sResult = title.replace('o', 'X');

                 sResult =                                                               sResult =

 

 

 

            e)  sResult = title + num1 + num2;              f)  sResult = title + (num1 + num2);

                 sResult =                                                               sResult =

 

 

 

            g)  iResult = Math.max(num2, num3);            h)  fResult = Math.pow(num4, 3);

        iResult =                                    fResult =

 

            i)  iResult = Math.floor(val2);                      j)  fResult = Math.pow(title.length(), 2) +                     iResult =                                                                               num3 * Math.sqrt(num3 / num4);

                                                                                        fResult =

 

 

2.For exercises 1 to 5, indicate the output that will be produced.  Assume the following declarations:

final int MAX=25, LIMIT=100;

int num1 = 12, num2 = 25, num3 = 87;

 

Exercises

Output

Exercise 1

if (num3-num2 > 2*MAX)

            System.out.println("apple");

else

            System.out.println("orange");

 

 

Exercise 2

if (LIMIT+num3 <=150)

{

            System.out.println("apple");

            System.out.println("orange");

}

else

            System.out.println("pear");

 

 

Exercise 3

if (num3 == 87)

{

            if (num2 != MAX)

                        System.out.println("apple");

}

            else

            System.out.println("orange");

System.out.println("pear");

 

Exercise 4

if (num3 <= MAX)

            System.out.println("apple");

else

            if (num2*LIMIT != 3298)

                        System.out.println("orange");

 

Exercise 5

if (LIMIT % MAX == 3)

            System.out.println("apple");

else

            if (num2==MAX)

                        System.out.println("orange");

            else

                        System.out.println("pear");

 

 

 

 

3.Write an application that asks for a person's name and then prints it one time, then two times, then three times and then four times.

 

For example, if the user entered Mark:

Mark

Mark, Mark

Mark, Mark, Mark

Mark, Mark, Mark, Mark

 

/* NameRep.java

    Tutorial 2 - CSCI 1100/1202

    This program replicates a name once, twice, three and four times to the screen

*/

  import java.io.*;

  public class NameRep

  {

      public static void main (String args[]) throws IOException

      {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

      } //end main

  }//end class

 

 

4.Write an application that asks the user for a title of a movie and asks the user to enter a value of either 1 or 2. If the user enters 1, then change all occurrences of 'a', 'e' and 'i' in the movie title with 'B'. If the value is 2, then change all occurrences of 'o' and 'u' with 'C'.  Print the changed movie title.

 

For example, if the user entered "The Lord of the Rings" and the value 2, the output would be:

            "The LCrd Cf the Rings"

 

/* TitleReplace.java

    Tutorial 2 - CSCI 1100/1202

    This program replaces the vowels in a movie title with either 'B' or 'C'

*/

  import java.io.*; //import java's input/output class

  public class TitleReplace

  {

      public static void main (String args[]) throws IOException

      {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     } //end main

  }//end class