Starting from:

$30

CPT120-Tutorial 6 Renovation Project Manager Solved

5.1 (Hypothetical scenario) You turn up to your programming job one fine afternoon and find out that a programmer in your coding team has quit without giving any notice and all that the company has of the code that they were working on is the following “screenshot” (from which you would not normally be able to copy and paste):
import javax.swing.JOptionPane;

public class RenovationProjectManager {    public static void main(String[] args) {

      double wallArea = 0, cost, height, length, costPerSqm;       int selection;

      String tempInput, wallNames;       String menu = "Menu:\n";

      menu += "1. Calculate paint required for a wall\n";       menu += "2. Calculate paint required for project";       tempInput = JOptionPane.showInputDialog(menu);       while (tempInput != null) {

         selection = Integer.parseInt(tempInput);          if (selection == 1) {

            costPerSqm = Double.parseDouble(JOptionPane.showInputDialog("Enter cost per sq.m ($)"));

            tempInput = JOptionPane.showInputDialog("Enter a wall name");

            height = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall height (m)"));             length = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall length (m)"));             wallArea = height * length;             cost = wallArea * costPerSqm;             JOptionPane.showMessageDialog(null,

                  "Cost to paint " + tempInput + " wall of " + wallArea + " sq.m. is $" + cost);

         } else if (selection == 2) {

            costPerSqm = Double.parseDouble(JOptionPane.showInputDialog("Enter cost per sq.m ($)"));             wallNames = "";             wallArea = 0;             cost = 0;

            tempInput = JOptionPane.showInputDialog("Enter a wall name (cancel to finish)");             while (tempInput != null) {

               height = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall height (m)"));                length = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall length (m)"));

               wallArea += height * length;                wallNames += tempInput + ", ";

               tempInput = JOptionPane.showInputDialog("Enter a wall name (cancel to finish)");

            }             cost = wallArea * costPerSqm;             JOptionPane.showMessageDialog(null,                   "Cost to paint " + wallNames + "wall(s) of " + wallArea + " sq.m. is $" + cost);          } else {

            JOptionPane.showMessageDialog(null, "Invalid choice!");

         }

         tempInput = JOptionPane.showInputDialog(menu);

      }

   }

}
Your first task is to run the code in Eclipse and add some high-level, plain-English comments, explaining what the different parts of the code are doing. Speak to your group tutors via forums/tutor chats for advice.

5.2.  Create a method for each of the (two) menu options and then move the code relevant to each menu option from the main method to the relevant methods that you created. In place of the original code that was moved, you must have a call to the method that would now do the work instead.

 you must ensure that:

1.  Only method definitions are at the class level (e.g. do not create class/object member variables).

2.  Only the ‘main’ method definition has any mention of ‘static’. All method methods should be explicitly ‘public’ and not static.

5.3.  Create a constructor method and then move all of the code in the ‘main’ method to the constructor method instead. Add comments to the code and explain what further changes you needed to make to the code in 5.2.

5.4.: Add a comment to your main method explaining what we should have in the main method and what we should not.

More products