I have solved some questions, please check if my answers are correct

Hi every Java aces,

I have solved some programming questions, please check if my answers are correct. Thanks

Here are the questions and the answers to each question

(a) Write a method called add that takes three integers as parameters and returns an

integer that is the sum of the three parameters.

(b) Write a method called max that takes three integers as parameters and returns the

largest of the three integers.

(c) Write a method called printMax that takes three integers as parameters and prints

the maximum value to the screen. The method should return nothing. The method

should print the words \The maximum value is " followed by the maximum value,

followed by a new-line character. You should try to use the max method that you

wrote earlier.

(d) Write a method called min that takes three integers as parameters and returns the

smallest of the three integers.

(f) Write a method called ticketPrice that takes one integer parameter representing

the age of a person, and returns the price of a movie ticket in dollars (as a

oating

point value). Children under 6 get in for free (0.00), people that are 18 or under

pay 8.50, senior citizens (65 or older) pay 6.50, and all other people pay 10.00.

(g) Write a method called isDivisor that takes two integers as parameters and returns

true if the rst parameter is a divisor of the second parameter or false otherwise.

We say that m is a divisor of n, or that m divides n, if there is an integer k such

that n = k  m. Note that 1 divides every integer, and every integer divides i

itself .

here are my codes

public class Methods

{

// This is method (a)

public static int add (int a, int b, int c)

{

int sum = a + b + c;

return sum;

}

// This is method (b)

public static int max (int a, int b, int c)

{

int largest;

if (a > b)

{

if (a > c)

{ largest = a; }

else

{ largest = c;}

}

else

{

if (b < c)

{ largest = c; }

else

{ largest = b;}

}

return largest;

}

// This is method (c)

public static void printMax (int d, int e, int f)

{

int maxValue;

maxValue = max (d, e, f);

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

}

// This is method (d)

public static int min (int a, int b, int c)

{

int smallest;

if ( a < b )

{

if ( a < c )

{ smallest = a; }

else

{ smallest = c; }

}

else

{

if ( b > c )

{ smallest = c; }

else

{ smallest = b; }

}

return smallest;

}

// This is method (f)

public static double ticketPrice (int age)

{

double price;

if ( age < 6 )

{ price = 0.00; }

else

{

if ( age <= 18 )

{ price = 8.50; }

else

{

if ( age < 65 )

{ price = 10.00; }

else

{ price = 6.50; }

}

}

return price;

}

// This is method (g)

public static boolean isDivisor ( int m, int n )

{

boolean result;

int check1;

double check2;

check1 = n / m;

check2 = (double)n / m;

if ( check2 == check1 )

{ result = true; }

else

{ result = false; }

return result;

}

[3588 byte] By [Ace1a] at [2007-11-27 5:15:56]
# 1
@Op. Please use code formatting when you post code: http://forum.java.sun.com/help.jspa?sec=formattingKaj
kajbja at 2007-7-12 10:38:21 > top of Java-index,Java Essentials,Java Programming...
# 2
How would you check these, yourself?
Hippolytea at 2007-7-12 10:38:21 > top of Java-index,Java Essentials,Java Programming...
# 3
on number c), you forgot the newline.
Dalzhima at 2007-7-12 10:38:21 > top of Java-index,Java Essentials,Java Programming...
# 4

Here are my codes

// This is method (a)

public static int add (int a, int b, int c)

{

int sum = a + b + c;

return sum;

}

// This is method (b)

public static int max (int a, int b, int c)

{

int largest;

if (a > b)

{

if (a > c)

{ largest = a; }

else

{ largest = c;}

}

else

{

if (b < c)

{ largest = c; }

else

{ largest = b;}

}

return largest;

}

// This is method (c) ?

public static void printMax (int d, int e, int f)

{

int maxValue;

maxValue = max (d, e, f);

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

}

// This is method (d)

public static int min (int a, int b, int c)

{

int smallest;

if ( a < b )

{

if ( a < c )

{ smallest = a; }

else

{ smallest = c; }

}

else

{

if ( b > c )

{ smallest = c; }

else

{ smallest = b; }

}

return smallest;

}

// This is method (f)

public static double ticketPrice (int age)

{

double price;

if ( age < 6 )

{ price = 0.00; }

else

{

if ( age <= 18 )

{ price = 8.50; }

else

{

if ( age < 65 )

{ price = 10.00; }

else

{ price = 6.50; }

}

}

return price;

}

// This is method (g)

public static boolean isDivisor ( int m, int n )

{

boolean result;

int check1;

double check2;

check1 = n / m;

check2 = (double)n / m;

if ( check2 == check1 )

{ result = true; }

else

{ result = false; }

return result;

}

// This is method (h)

public static boolean isProduct ( double a, double b, double c )

{

boolean result;

if ( a == b*c )

{ result = true;}

else

{ result = false;}

return result;

}

Ace1a at 2007-7-12 10:38:21 > top of Java-index,Java Essentials,Java Programming...
# 5

To write cleaner code, you can replace:

public static boolean isProduct ( double a, double b, double c )

{

boolean result;

if ( a == b*c )

{ result = true;}

else

{ result = false;}

return result;

}

with

public static boolean isProduct ( double a, double b, double c ) {

return a == b * c;

}

As a side note, isProduct(1.0, 1.0/49.0, 49.0) is false.

Hippolytea at 2007-7-12 10:38:22 > top of Java-index,Java Essentials,Java Programming...
# 6

>...

>public static int max (int a, int b, int c)

> {

>int largest;

>

>if (a > b)

> {

>if (a > c)

> { largest = a; }

>

> else

>{ largest = c;}

>

>}

>

>else

> {

>if (b < c)

> { largest = c; }

>

> else

>{ largest = b;}

>

>}

>

>return largest;

>

> }

> ...

Man, what a lot of lines!

public static int max(int a, int b, int c) {

return a > b && a > c ? a : max(b, c, a);

}

orpublic static int max(int a, int b, int c) {

return Math.max(Math.max(a,b), c);

}

>public static boolean isDivisor ( int m, int n )

> {

>boolean result;

>

>int check1;

>double check2;

>

>check1 = n / m;

>check2 = (double)n / m;

>

>if ( check2 == check1 )

>{ result = true; }

>

>else

>{ result = false; }

>

> return result;

>

> }

Have a look at the modulo operator, a.k.a. the remainder operator:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html

prometheuzza at 2007-7-12 10:38:22 > top of Java-index,Java Essentials,Java Programming...