hts.StevenWood.com

[ Last Page ]

Methods Test Solutions 

  

  1. Give the corrected version of each of the following.

(7 marks)

 

 

Corrected version

a.

// find largest of 3 numbers

private boolean findBiggest ( long num1,                         long num2; num3 );
{

            long bigger = max( num1, num2 );

            biggest = max( bigger, num3 )

            return findBiggest;

}

// find largest of 3 numbers

private static long findBiggest( long num1,
                                 long num2, long num3) (no semi)
{

            long bigger = Math.max( num1, num2 );

            long biggest = Math.max( bigger, num3 ) ;

            return biggest;

}

b.

public void help {

            system.out.println( “To use …”) ;
            System.out.println( “this …”) ;
            System.out.println( ‘program…”);
            System.out.println( “etc…”);
            return ;
}

public static void help ( ) {

            System.out.println( “To use …”) ;
            System.out.println( “this …”) ;
            System.out.println( program…”);
            System.out.println( “etc…”);
            No return 
}

2.      Give the header line for each of the following methods.     (6 marks)

a.      The non-public method findArea receives three double values and returns the product of the values.

private static double findArea ( double x, double y, double z )           

b.      Method menu does not use any parameters and does not return a value:

public static void menu( )

c.      Method dayOfWeek receives a Date object and returns a String object:

public static String dayOfWeek ( Date d )

  1. Give 1 OR 2 program lines that will accomplish the described tasks (you do not have to declare variables):    (6 marks)
    1. Find the square root of the absolute value of the double type variable named value and display it on the monitor.

      System.out.println ( Math.sqrt( Math.abs ( value ) ) );
    2. Find the location of the first letter ‘e’ in the String object named userName and store it in a variable named number.

      int number = userName.indexOf( ‘e’ ) ;
    3. Set a boolean variable, sameString, to a value by comparing two strings, input1, and input2, to see if they are equal without distinguishing between upper and lower case letters. E.g. ‘hello’ would be equal to ‘HELLO’

      boolean sameString = input1.equalsIgnoreCase( input2 ); // or
      boolean sameString = input2.equalsIgnoreCase( input1 );

  2. a) Create a method named pounds that will receive a double value, representing a number of kilograms, and calculates and return equivalent number of pounds. Note that there are 2.2 pounds in every kilogram.    (3 marks)

 

public static double pounds ( double k ) {

            return k * 2.2 ;
}

b) Give the main method for a class that receives a positive value from user input for the number of kilograms and then converts that number to pounds using the method from part (a). Display the answer.

E.g.

Enter the number of kilograms: -15.6

Must be positive!

Enter the number of kilograms: 15.6

(10 marks)

The number of pounds is 34.32


public static void main (String [ ] args ) throws Exception{

 

InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader keyboard = new BufferedReader( isr );
System.out.print ( “Enter the number of kilograms: ” );
String input = keyboard.readLine();
double kilos = Double.parseDouble ( input );
while (kilos < 0 ) {
            System.out.println ( “Must be positive! ” );
            System.out.print ( “Enter the number of kilograms: ” );
            input = keyboard.readLine();
            kilos = Double.parseDouble ( input );
}
double weightInPounds = pounds( kilos );
System.out.println ();
System.out.println ( “The number of pounds is ” + weightInPounds );

}

 

 

[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+