import javax.swing.JOptionPane;
public class wallarea
{
// main method
public static void main(String[]args)
{
double area ;
String input;
int number = 1;
while (number == 1) // while loop
{
input = JOptionPane.showInputDialog("Enter the Lenght in feet of the wall");
int l = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the Height in feet of the wall");
int h = Integer.parseInt(input);
areaOfRoom(l,h);
input = JOptionPane.showInputDialog("Please Type in 1 to add measurements of another wall or 2 to quit");
number = Integer.parseInt(input);
}
System.exit (0);
}
// Method, to find Area of room to be covered
public static void areaOfRoom(int lenght, int height)
{
double areaOfwall = (lenght*height);
double areaOfwallTotal = ((areaOfwall *.10) + areaOfwall);
double numberOfRolls = (areaOfwallTotal / 35);
double roundNumRolls = Math.ceil(numberOfRolls);
JOptionPane.showMessageDialog(null, "The number of wallpaper rolls needed is " + roundNumRolls + ".");
}
}
Help with multiple inputsObtaining and storing multiple inputs with JOptionPanes
Page 1 of 1
12 Replies - 240 Views - Last Post: 10 March 2013 - 09:40 PM
#1
Help with multiple inputs
Posted 08 March 2013 - 09:47 PM
Replies To: Help with multiple inputs
#2
Re: Help with multiple inputs
Posted 09 March 2013 - 01:53 AM
Follow Java naming conventions: Class titles are capitalized.
Your program functions properly the way it is. You'll have to provide more details about what else you need it to do in order to get help.
This post has been edited by GregBrannon: 09 March 2013 - 02:02 AM
#3
Re: Help with multiple inputs
Posted 09 March 2013 - 02:33 AM
to put together a little bit of psuedo for you to consider on how I would go about this, which may lead you to your answer, and likely a bit of brain pain..
areaDoor = (3*7) // Standard door area? areaWindow = (3.5*4.5) //Standard window area? roomLength = input(length of room) roomWidth = input(width of room) roomHeight = input(height of room) numDoors = input(number of doors) numWindows = input(number of windows) wallArea = (roomLength * roomHeight * 2) + (roomWidth * roomHeight * 2) wallArea = (wallArea - ((areaDoor * numDoors) + (areaWindow * numWindows))) numRolls = (Your math with wallArea variable)
That will get your rolls to a 'per room' stand point and cut your measuring down a bit, then your loop can be the same, you will just need to declare an array before the loop, and each time you go through the loop, you can use the array[].add function to add the calculated value to your array. Once you receive number=2 you leave the loop, then you'll have to loop one more time through each element in your array and add each of those values together (you could save this last step if you dont use an array and just add each calculation to an integer variable as you go and output this value at the end) however without the array, you don't have the option of something such as
for each in array
output("Room 1: " + array[item] + " rolls.")
total = total + array[item]
next item
output("Total: " + total + " rolls.")
given you may not be covering every room in the same paper
#4
Re: Help with multiple inputs
Posted 09 March 2013 - 08:48 PM
GregBrannon, on 09 March 2013 - 01:53 AM, said:
Follow Java naming conventions: Class titles are capitalized.
Your program functions properly the way it is. You'll have to provide more details about what else you need it to do in order to get help.
Hi GregBrannon, you are right, in fact I wanted to change the title, but I wasn't sure how to. I am trying to create a program that calculates the number of rolls of wallpaper needed. Once the measurements of each wall in the room has been entered it should calculate the number of rolls.
Thanks
J
Takk, on 09 March 2013 - 02:33 AM, said:
to put together a little bit of psuedo for you to consider on how I would go about this, which may lead you to your answer, and likely a bit of brain pain..
areaDoor = (3*7) // Standard door area? areaWindow = (3.5*4.5) //Standard window area? roomLength = input(length of room) roomWidth = input(width of room) roomHeight = input(height of room) numDoors = input(number of doors) numWindows = input(number of windows) wallArea = (roomLength * roomHeight * 2) + (roomWidth * roomHeight * 2) wallArea = (wallArea - ((areaDoor * numDoors) + (areaWindow * numWindows))) numRolls = (Your math with wallArea variable)
That will get your rolls to a 'per room' stand point and cut your measuring down a bit, then your loop can be the same, you will just need to declare an array before the loop, and each time you go through the loop, you can use the array[].add function to add the calculated value to your array. Once you receive number=2 you leave the loop, then you'll have to loop one more time through each element in your array and add each of those values together (you could save this last step if you dont use an array and just add each calculation to an integer variable as you go and output this value at the end) however without the array, you don't have the option of something such as
for each in array
output("Room 1: " + array[item] + " rolls.")
total = total + array[item]
next item
output("Total: " + total + " rolls.")
given you may not be covering every room in the same paper
Ok, this is so helpful, and I can now see a little clearer... I shall study and play around with the code a little more... If I get stuck again...can I ask???
#5
Re: Help with multiple inputs
Posted 09 March 2013 - 10:27 PM
#6
Re: Help with multiple inputs
Posted 10 March 2013 - 04:00 AM
#7
Re: Help with multiple inputs
Posted 10 March 2013 - 10:02 AM
I have reworked the code a little, this time I have separated into methods a little more.
However, my output is putting in the calculations, its giving me just 0 values..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
public class WallpaperEstimatorProject extends JFrame implements ActionListener
{//begin public class WallpaperEstimatorProject extends JFrame implements ActionListener
//construct components
JTextPane displayPane = new JTextPane();
DecimalFormat format = new DecimalFormat("######.##");
//declare and intialize data
boolean firstRun = true;
int arrayLength;
double [] totalSquareFootage = new double[50];
String strNumberWalls;
int numberWalls;
String strWallLength;
double wallLength;
String strWallHeight;
double wallHeight;
String strRollCost;
double rollCost;
int wallNumberCounter = 1;
double totalArea = 0;
double totalWallpaperCharge =0;
//construct instance
public WallpaperEstimatorProject()
{
super("Wallpaper Estimator");
}
public Container createContentPane()
{// begin public Container createContentPane()
//construct and populate main panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout (new FlowLayout());
displayPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(displayPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setPreferredSize(new Dimension(190, 300));
mainPanel.add(scrollPane);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(mainPanel);
return c;
} //end public Container createContentPane()
public JTextPane addTextToTextPane()
{//Begin public JTextPane addTextToTextPane()
Document doc = displayPane.getDocument();
try
{
//The following lines generate JTextPane attributes on-the-fly
//to simplify adding text to the JTextPane
StyleContext style = StyleContext.getDefaultStyleContext();
AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);
displayPane.setParagraphAttributes(set,false);
//clear previous text
doc.remove(0,doc.getLength());
for(int lcv = 0; lcv < arrayLength; lcv++)
{
doc.insertString(doc.getLength(), String.valueOf(totalSquareFootage[lcv])+ "\n" ,set);}
if(firstRun)
System.out.println("Program Started.");
else
{
double calculate = calculate();
doc.insertString(doc.getLength(),"Number of wallpaper rolls needed: " + String.valueOf(format.format(calculate))+ "\n",set);
doc.insertString(doc.getLength(),"Total cost of wallpaper: " +(calculate() * rollCost)+ "\n",set);}
firstRun = false;
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert measurements.");
}
return displayPane;
}//end public JTextPane addTextToTextPane()
public JMenuBar createMenuBar()
{ // begin public JMenuBar createMenuBar()
// Create an instance of the menu
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
//Construct and add the File menu
JMenu mnuFile = new JMenu("File");
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
//Construct and add the menu command to add grades
JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Measurements");
mnuFileNewGrades.setMnemonic(KeyEvent.VK_W);
mnuFileNewGrades.setDisplayedMnemonicIndex(0);
mnuFile.add(mnuFileNewGrades);
mnuFileNewGrades.setActionCommand("NewMeasurements");
mnuFileNewGrades.addActionListener(this);
//Construct and add the Exit Command
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
return mnuBar;
} // end public JMenuBar createMenuBar()
public void actionPerformed(ActionEvent e)
{ //begin public void actionPerformed(ActionEvent e)
String arg = e.getActionCommand();
if(arg == "NewMeasurements")
{//begin if new measurement is clicked
//Resets the array so no values get accidentally calculated
for(int lcv = 0 ; lcv < totalSquareFootage.length; lcv++)
totalSquareFootage[lcv] = 0.0;
//receives number of walls to calculate
strNumberWalls = JOptionPane.showInputDialog("Please enter the number of walls to wallpaper." );
numberWalls = Integer.parseInt(strNumberWalls);
//receives measurement and price inputs until counter reaches specified number of walls
while (wallNumberCounter<= numberWalls)
{
strWallLength = JOptionPane.showInputDialog("Please enter length in square feet for the room # " + wallNumberCounter );
wallLength = Double.parseDouble (strWallLength);
strWallHeight = JOptionPane.showInputDialog("Please enter height in square feet for the room # " + wallNumberCounter );
wallHeight = Double.parseDouble (strWallHeight);
totalSquareFootage[wallNumberCounter] = (wallLength * wallHeight); //calculate wall's area and holds result to the array
strRollCost = JOptionPane.showInputDialog("Please enter the cost per roll of the wallpaper you have selected." );
rollCost = Double.parseDouble(strRollCost);
wallNumberCounter = wallNumberCounter + 1;//increment wallNumberCounter
}
addTextToTextPane();
}//end if NewMeasurement is clicked
//if exit is clicked
if(arg == "Exit")
{
System.out.println("Ciao Ciao.");
System.exit(0);
}
} // End public void actionPerformed(ActionEvent e)
public double calculate()
{
//sum the values of the array
double sum = 0, calculate;
int totalCount = 0;
for(int lcv = 0 ; lcv < arrayLength; lcv++)
{
if(totalSquareFootage[lcv] > 0)
sum += totalSquareFootage[lcv];
}
// calculate rolls
double totalWallpaper = ((totalArea *.10)+ totalArea);
double numberOfRolls = (totalWallpaper / 35);
double totalNumberOfRolls = Math.ceil(numberOfRolls);
calculate = totalNumberOfRolls;
return calculate;
}
//main method executes at run time
public static void main(String[] args)
{ //begin public static void main(String[] args)
WallpaperEstimatorProject m = new WallpaperEstimatorProject();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setJMenuBar( m.createMenuBar());
m.setContentPane( m.createContentPane());
m.setSize(200, 375);
m.setVisible(true);
JOptionPane.showMessageDialog(null, "To calculate, Hit File >> Enter New Measurements.\n" +
"To end input, enter -1 in the grade value box.",
"Howdy", JOptionPane .INFORMATION_MESSAGE);
} //end public static void main(String[] args)
}//end public class WallpaperEstimatorProject extends JFrame implements ActionListener
#8
Re: Help with multiple inputs
Posted 10 March 2013 - 07:54 PM
That being said, in your calculate function above, when you're looping to arrayLength, arrayLength is 0 because it has not been assigned a value, you're looking for totalSquareFootage.length here, and below your math in the same function, you'll need to assign totalArea = sum; before you continue your calculations. You'll have to test a few cases here as I only did one wall to find the issue, there may still be bugs elsewhere.
As a general suggestion, and I apologize if I'm incorrect given I dont know java specifically... I wouldn't necessarily always put values in global variables then call a function with no parameters, it makes debugging a bit more stressful than it has to be on you. My suggestion would be something along the lines of
overAllArea += calculate(height, length, cost)
hope this helps.. keep at it.
#9
Re: Help with multiple inputs
Posted 10 March 2013 - 08:20 PM
for(int lcv = 0; lcv < arrayLength; lcv++)
{
doc.insertString(doc.getLength(), String.valueOf(totalSquareFootage[lcv])+ "\n" ,set);}
if(firstRun)
System.out.println("Program Started.");
else
{
double calculate = calculate();
doc.insertString(doc.getLength(),"Number of wallpaper rolls needed: " + String.valueOf(format.format(calculate))+ "\n",set);
doc.insertString(doc.getLength(),"Total cost of wallpaper: $" +(calculate() * rollCost)+ "\n",set);}
firstRun = false;
}
and here
public double calculate()
{
//sum the values of the array
double sum = 0, calculate;
int totalCount = 0;
for(int lcv = 0 ; lcv < arrayLength; lcv++)
{
if(totalSquareFootage[lcv] > 0)
sum += totalSquareFootage[lcv];
}
// calculate rolls
double totalWallpaper = ((totalArea *.10)+ totalArea);
double numberOfRolls = (totalWallpaper / 35);
double totalNumberOfRolls = Math.ceil(numberOfRolls);
calculate = totalNumberOfRolls;
return calculate;
}
#10
Re: Help with multiple inputs
Posted 10 March 2013 - 08:48 PM
Ok, I think I have figured what you are trying to tell me. Now the code works and outputs the correct price, but still does not output the number of rolls needed for the job, any suggestions?
#11
Re: Help with multiple inputs
Posted 10 March 2013 - 08:53 PM
Line 7: for(int lcv = 0 ; lcv < totalSquareFootage.length; lcv++)
arrayLength was =0, so it was never adding anything to the sum variable
Line 12: totalArea = sum;
My inputs were rooms=1, height=10, length=10, cost=1
the output was rolls: 4, cost: 4
My inputs were rooms=1, height=15, length=25, cost=1
the output was rolls: 12, cost: 12
I was admittedly a bit lost in the flow of everything, but was able to get two correct results for a single room, I'm pretty sure if you get the top instance resolved also, you should have what you were after.
After looking back at the code, arrayLength is never set while your rooms are being calculated, despite my changes you may be able to update arrayLength correctly and have everything fall together but I haven't done any checking past that. Hope this helps some.
#12
Re: Help with multiple inputs
Posted 10 March 2013 - 09:03 PM
|
|

New Topic/Question
Reply



MultiQuote



|