Posts

Chapter 4&5 Homework Answers

Image
C H A P T E R  4 FUNDAMENTAL DATA TYPES Home Work Answers •• E4.4 Write a program that prompts the user for two integers and then prints • The sum • The difference • The product • The average • The distance (absolute value of the difference) • The maximum (the larger of the two) • The minimum (the smaller of the two) Hint: The max and min functions are declared in the Math class. •• E4.5 Enhance the output of Exercise E4.4 so that the numbers are properly aligned: Sum: 45 Difference: -5 Product: 500 Average: 22.50 Distance: 5 Maximum: 25 Minimum: 20   ••• E4.13 Write a program that reads a number between 1,000 and 999,999 from the user, where the user enters a comma in the input. Then print the number without a comma. Here is a sample dialog; the user input is in color: Please enter an integer between 1,000 and 999,999: 23,456...

Chapter 3 Homework Answers

Image
Chapter 3 – Implementing Classes Home Work Answers R3.16 Consider the following implementation of a class Square: public class Square { private int sideLength; private int area; public Square(int initialLength) { sideLength = initialLength; area = sideLength * sideLength; } public int getArea() { return area; } public void grow() { sideLength = 2 * sideLength; } } What error does this class have? How would you fix it? public class Square {     private int sideLength;     public int area;     public Square  (int initialLength) {         sideLength = initialLength;     }     // to Correct it     public int getArea ( ) {         area = sideLength * sideLength;         return area;     } } E3.9   Add a method printReceipt...