Question

In: Computer Science

For the second part of the Lab, improve the following Java program with functions. You should...

  1. For the second part of the Lab, improve the following Java program with functions. You should look for places in the code where similar statements are repeatedand think about how towrite function(s) to replace those statements. Unlike the first part of the Lab, in this exercise you will probably want to create one or more functions that return a value, and use the returned values in the rest of the program.

This is the Painting.javasource file for you to modify (bluelines are my additions):

public class Painting

{

   public static void main(String[] args)

   {

        double width, length, wallArea, ceilingArea;

        final double HEIGHT = 8;

      

        System.out.println("Calculation of Paint Requirements");

        

        System.out.print("Enter room length: ");

        length = Keyboard.nextDouble();

          // these 2 lines will make a nice function, promptDouble,

          // then replace those 2 lines with this single line:

          // length = promptDouble("Enter room length: ");

        System.out.print("Enter room width: ");

        width = Keyboard.nextDouble();

          // these 2 lines should be replaced in the same way:

          // width = promptDouble("Enter room width: ");

        

        wallArea = 2 * (length + width) * HEIGHT; // ignore doors

          // this line should use a perimeterfunction instead, as in:

          // wallArea = perimeter(length, width) * HEIGHT;

        ceilingArea = length * width;

          // this line should use a new function called area,

          // similar to perimeter, like so:

          // ceilingArea = area(length, width);

        System.out.println("The wall area is " + wallArea +

             " square feet.");

        System.out.println("The ceiling area is " + ceilingArea +

             " square feet.");

   }

}

Your job in this Lab is to write these three functions and make the above changesin the Java program Painting.java.

Solutions

Expert Solution

import java.util.Scanner;

public class Painting {

    public static Scanner Keyboard = new Scanner(System.in);

    public static double promptDouble(String s) {
        System.out.print(s);
        return Keyboard.nextDouble();
    }

    public static double perimeter(double length, double width) {
        return 2 * (length + width);
    }

    public static double area(double length, double width) {
        return length * width;
    }

    public static void main(String[] args) {
        double width, length, wallArea, ceilingArea;
        final double HEIGHT = 8;
        System.out.println("Calculation of Paint Requirements");
        length = promptDouble("Enter room length: ");
        width = promptDouble("Enter room width: ");
        wallArea = perimeter(length, width) * HEIGHT;
        ceilingArea = area(length, width);
        System.out.println("The wall area is " + wallArea + " square feet.");
        System.out.println("The ceiling area is " + ceilingArea + " square feet.");
    }
}


Related Solutions

Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
For this lab you will write a Java program that plays a simple Guess The Word...
For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...
In the second task, you will write a Java program that validates an input to see...
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab. 1. Prompt the user to enter the email address. 2. Read the user input as a String. 3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the...
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
NEED IN JAVA ECLIPSE For this lab, you will create a program that uses method overloading...
NEED IN JAVA ECLIPSE For this lab, you will create a program that uses method overloading and a switch statement. Requirements: The Patapsco River Valley Running Festival needs a program that will allow them to calculate the cost of the entry fee for each group of entrants. Once the cost is calculated, it is then displayed to the user. The program also totals up the total amount of money that will be collected and displays the total at the very...
Write a C, C++ or Java program to implement each of the following functions. For each...
Write a C, C++ or Java program to implement each of the following functions. For each function, use a long long datatype for all integer variables and a double datatype for all decimal variables. Permutations(N,X): This function returns the number of ways X objects can be drawn from N objects in a particular order. Combinations(N,X): This function returns the number of ways X objects can be drawn from N objects ignoring the order in which the objects are drawn. Binomial(N,P,X):...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory function (¬?) 2) logical and (? ∧ ?) 3) logical inclusive or (? ∨ ?) 4) implication function (? → ?) 5) biconditional (? ↔ ?) 6) logical exclusive or (? ⨁ ?) You can find the logical definitions for the above functions as given by Bertrand Russell in your text. You need to write a method (function) for each corresponding logical function. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT