Results 1 to 3 of 3
Thread: Need help with program: arrays
- 04-01-2009, 07:00 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Need help with program: arrays
Hello, this is a program given to me and I need some help writing the code for it.
You are to create a well‐documented, well‐designed Java application that displays information for of a House and its rooms. The user/House owner would like to enter the names of the rooms in the house at the beginning of the execution of the application. Once the names are entered, the application should continue to ask the user to enter the dimensions of the room i.e. Length and width.
At the conclusion of the execution, a summary should be provided. The summary should include: The names of all the rooms; the dimensions of the rooms entered. Additionally, the user should be display the Total Size, Average Room Size and the Biggest Room.
For this program I need to use arrays. How many arrays should I create...4?
*owner
*length
*width
*roomName
Also, should I create the arrays in the main method?
import javax.swing.JOptionPane;
public class House {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] ownerName = new String;
int numofRooms;
int roomLength;
int roomWidth;
String roomName;
numofRooms= integer.parseInt(JOptionPane.showInputDialog("How many rooms in this house?"));
string[] rooms =new string[numofRooms];
roomName= JOptionPane.showInputDialog("Enter a name for room#1?");
roomLength= integer.parseInt(JOptionPane.showInputDialog("What is the lenght of the" + roomName);
roomWidth= integer.parseInt(JOptionPane.showInputDialog("What is the width of the" + roomName);
}
}
- 04-01-2009, 07:22 PM #2
Member
- Join Date
- Sep 2008
- Posts
- 7
- Rep Power
- 0
First off I was taught in all my programming classes to only use class and method calls in your main();
I would create an array for the room names
String[] roomName = new String[the num of rooms]
Since you only need to know the total size, biggest room and avg room size.
Create a second array of the area of the room sizes.
int[] sizes = new int[same size as room name];
Then right after the user inputs the length and width multiply them and store them in the array.
What you doing is creating parallel arrays. so index 0 of roomName and sizes refer to the same room.
So first ask the number of rooms.
Then get the room names and length and width.
that get all your data in. then just use three more for loops one to find the biggest another to sum all the areas up, and a third to average them.Java Code:for(int i = 0; i < numberOfRooms; i++){ int length; int width; ask the room name and store in roomName[i] ask the length and width and store in length and width sizes = (length * width)
- 04-02-2009, 05:59 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
why dont use int ttlSize = 0,
and increment ttlSize by sizes in each looping
and add two more variables biggestRoom, biggestRoomSize to store the Name of room, compare and update them in each looping?
Similar Threads
-
Can someone please help fix this code so the program works? (2D arrays)
By busdude in forum New To JavaReplies: 2Last Post: 11-18-2008, 10:44 PM -
Need help with Arrays
By dietgal in forum New To JavaReplies: 21Last Post: 10-08-2008, 01:59 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
Need help with a program for high school regarding multi-dimensional arrays.
By Torque in forum New To JavaReplies: 2Last Post: 01-07-2008, 07:45 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks