- Follow the materials under CanvasModulesWeek 6
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: ;menu += 1. Calculate paint required for a wall ; 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. |
Page 1 of 2
5.2. With the help of your group tutors via forums/tutor chats: 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.
For this week, you must ensure that:
- Only method definitions are at the class level (e.g. do not create class/object member variables).
- Only the main method definition has any mention of static. All method methods should be explicitly public and not static.
- With the help of your group tutors via forums/tutor chats: 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.
- With the help of your group tutors via forums/tutor chats: Add a comment to your main method explaining what we should have in the main method and what we should not.
- Add comments in the style required by Assignment 2. See rubric in section 9 of the Assignment 2 PDF.
Reviews
There are no reviews yet.