Minnumber1number2 quotient

This preview shows page 4 - 6 out of 7 pages.

Math.min(number1,number2); quotient = (double)number1/(double)number2; System.out.println("Larger: " + Math.max(number1, number2)); System.out.println("Difference:" + difference); System.out.println("Quotient:" + quotient); (15 marks) } private static String readInput() { try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); return in.readLine(); } catch (IOException e) {} return ""; } } Question 5 (15 marks) Complete the implementation of the FruitProfile class for building simple fruit profiles in a grocery store. The application Q5 makes use of the FruitProfile class as shown below. public class Q5 { public static void main(String[] args) { FruitProfile fruit1 = new FruitProfile("apple", 10); FruitProfile fruit2 = new FruitProfile("pear", 16); System.out.println(fruit1.toString()); fruit1.add(12); System.out.println("After adding"); System.out.println(fruit1.toString()); System.out.println(); System.out.println(fruit2.toString()); fruit2.subtract(5); System.out.println("After subtracting"); System.out.println(fruit2.toString()); System.out.println("There are currently " + fruit1.getAmount() + " " + fruit1.getName() + "(s) in store." ); } } The application Q5 shown above uses the FruitProfile class to create two FruitProfile objects. When a FruitProfile object is constructed, the name of the fruit (a String ) and the amount in store (an int ) are both specified. Given a correct implementation of the FruitProfile class, the output from the application above should be exactly as shown below. C:\> java Q5 Fruit name: apple Amount: 10 After adding Fruit name: apple Amount: 22 Fruit name: pear Amount: 16 After subtracting Fruit name: pear Amount: 11 There are currently 22 apple(s) in store.
Image of page 4

Subscribe to view the full document.

Complete the implementation of the FruitProfile class from the skeleton file given below. public class FruitProfile { // instance variables private String name; private int amount; //constructor public FruitProfile(String inputName, int inputAmount){ name = inputName; amount = inputAmount; } // add(): adds input amount to current amount public void add(int inputAmount) { amount += inputAmount; } // subtract(): subtracts input amount from current amount public void subtract(int inputAmount) { amount -= inputAmount; } // getName : returns name of fruit public String getName() { return name; } // getAmount : returns amount of fruit public int getAmount() { return amount; } // toString : returns String representation of fruit and current amount public String toString() { return "Fruit name: " + name + "\n" + "Amount: " + amount; } } (15 marks) Question 6 (10 marks) (a) Evaluate the following boolean expressions (i) (2 > 3) || (4 == 4) && !((4>5) || (2<3)) || (9!=2) true (1 mark) (ii) 3==4 && 3 != 4 || !(3 != 3) && !(4 != 4) || 5 <= 4 true (1 mark) (iii) ! ( ! (4 != 6) && !(5 >4)) true (1 mark) (b) DeMorgan's Law consists of the following two equivalences: !(A && B) == !A || !B !(A || B) == !A && !B Use DeMorgan's Law to help simplify the following expression.
Image of page 5
Image of page 6
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • System.out.println, FruitProfile

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern