help with simple java program
hi there,
i have recently started to learn java. and i have been given these simple java programs to get started with.
could and one give me any idea on how to solve these programs as they have blown my head away.
Write a program Wallcover_estimate.
This program must contain at least two methods (main and one or more other method(s)).
Write a method that calculates the wall area of a room. It should take three parameters – the width, length and height of a room. This method must then calculate the wall area and output the result to the screen.
The method should be called repeatedly from main until the user indicates that they wish to stop calculating room wall areas.
(Area of wall covering = (2*(length * height)) + (2* (width * height)).
if any one could solve and guide me through this.
thanks
Re: help with simple java program
Do you have any code/thoughts on how this problem can be solved? If you genuinely want to learn Java, I don't want to solve this problem for you.
Here are some pointers, though:
Quote:
The method should be called repeatedly from main until the user indicates that they wish to stop calculating...
This indicates you'll need a loop of some kind. There are 2 kinds of loops (well, 3, but 2 are basically the same): for, while, and do-while.
Which one is most appropriate for this situation?
Quote:
write a method that calculates wall area... Take three parameters, width length, height
This information is what the second method should do, and basically lays out what the method header/signiture should look like. What data types are appropriate here? Obviously boolean, character, and String won't do too well. What about int, long, float, or double?
As a side note, you'll want to declare this second method as being static. This is done by putting the "static" keyword in the signature. A public declaration would also be nice, but not necessary.
Code :
//ex:
public static void Foo()
{}
Lastly, to get user input, I'd recommend using the Scanner class, and the System.out stream for output.
To print out, you can use System.out.println("Text"); (replace Text with a String you want printed out).
Because Java auto-casts all other primitive data types to Strings, and the plus operator for Strings is defined to concatenate, you can do things like this to output numbers:
Code :
//ex:
int a = 5*5;
System.out.println("5*5 = " + a);
The Java API may be difficult to understand, especially if you don't know what to look for. Because Scanner is an object, you must create one before you can use it. It can take input from many sources, but you'll want to use System.in to get input from the user.
Code :
Scanner input = new Scanner(System.in);
There are a few methods you can use to get different number types:
Code :
int a = input.nextInt();
double b = input.nextDouble();
The special thing about reading in from the System.in stream is that it will automatically wait until something is typed before reading it in.
Hope that's enough to get you started. Reply if you still need help :)
Re: help with simple java program
hi there,
thank you very much on the points and help that you gave me with the wall area program. i have managed to solve the probem with your help. once again really appretiate your help and guidence.
thanks alot
Re: help with simple java program
my solution is for the wall area program is.....
// calculating area of wall covering of a room
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("Please Type in the Lenght of the Room");
int l = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Height of the Room");
int h = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Width of the Room");
int w = Integer.parseInt(input);
areaOfRoom(l,h,w);
input = JOptionPane.showInputDialog("Please Type in 1 to find the Area of Room + Please type 2 to quit");
number = Integer.parseInt(input);
/* if (lenght<0)
JOptionPane.showMessageDialog(null, "Lenght Can not be negative");
else if (height<0)
JOptionPane.showMessageDialog(null, "Height Can not be negative");
else if (width<0)
JOptionPane.showMessageDialog(null, "Width Can not be negative");
else
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + area);*/
}
System.exit (0);
}
// Method, to find Area of room to be covered
public static void areaOfRoom(int lenght, int height, int width)
{
double areaOfRoom =( 2 * (lenght*height)) + (2 * (width* height));
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + areaOfRoom);
}
}
Re: help with simple java program
hi there,
i have started to work on another program. it is a gym member database. where members registration details are to be added.
A Gym member record consists of membership number, name, postcode and fee.
there has to be a class definition to represent Gym membership details.
With a main method that declares and initialises two new Gym membership objects.
Write a method to request data items from the user to fill both the new Gym member objects.
and a method to display to the screen the names and membership numbers of both members.
Example data entry ( “Z123456”, “Paul Jones”, “HN78HG”, 250);
i have written what i could but when i compile the program it comes up with errors that i am having a problem understanding.
could any one HELP me through with this program.
thanks
import javax.swing.JOptionPane;
import java.io.*;
public class gym
{
public static void main (String [] args) throws IOException
{
int Counter=0;
member[] members = new members[45];
Controller(members, Counter);
}
public static void Controller(members[] values, int Counter) throws IOException
{
int Control=Integer.parseInt(JOptionPane.showInputDial og("MeNu\n" + "0 To Quit\n" + "1 to Add new employee" + "2 to Print the Employee" + "3 to Search\n"));
while (Control!=0)
{
if (Control==1)
{
for (int count=0; count<1; count++)
{
values[Counter]=new members();
String name=JOptionPane.showInputDialog("Name of member", "member");
int number=JOptionPane.showInputDialog("gym number", "gym number");
int postcode=Integer.parseInt(JOptionPane.showInputDia log("postcode","postcode"));
values[Counter]= new Database(name, number, postcode);
Counter++;
}
} // end of if 1
else if (Control==2)
{
for (int count=0; count<Counter; count++)
{
values[count].showValues();
}
}
else if (Control==3)
{
int DoSearch=Integer.parseInt(JOptionPane.showInputDia log("Searching\n" + "1 to Search member\n" + "2 to Search gym number\n" + "3 to Search postcode"));
if (DoSearch==1)
{
String nameSearching=JOptionPane.showInputDialog("Search Name of member", "Name of member");
int result=nameSearch(values, nameSearching , Control);
if (result==-5)
System.out.println("member That you Searched -> Not Found");
else
{
System.out.println(nameSearching + "\nis found in position\n" + (result+1));
values[result].showValues();
}
}
else if (DoSearch==2)
{
int numberSearching=JOptionPane.showInputDialog("Searc h gym number", "Search gym number");
int result=numberSearch(values, numberSearching , Control);
if (result==-5)
System.out.println("number That you Searched -> Not Found");
else
{
System.out.println(numberSearching + "\nis found in position\n" + (result+1));
values[result].showValues();
}
}
else if (DoSearch==3)
{
int postcodeSearching=Integer.parseInt(JOptionPane.sho wInputDialog("Search postcode", "Search postcode"));
int result=number(values, numberSearching , Control);
if (result==-5)
System.out.println("postcode That you Searched -> Not Found");
else
{
System.out.println(postcodeSearching + "\nis found in position\n" + (result+1));
values[result].showValues();
}
}
}
Control=Integer.parseInt(JOptionPane.showInputDial og("MeNu\n" + "0 To Quit\n" + "1 to Add new member\n" + "2 to Print gym number\n" + "3 to Search postcode\n"));
}
}
public static int nameSearch(members[] values, String Thekey, int Size)
{
for (int y=0; y<Size; y++)
{
if (values[y].name.equals(Thekey))
{
return y;
}
}
return -5;
}
public static int numberSearch(members[] values, int Thekey, int Size)
{
for (int h=0; h<Size; h++)
{
if (values[h].number.equalsIgnoreCase(Thekey))
{
return h;
}
}
return -5;
}
public static int postcode(members[] values, int Thekey, int Size)
{
for (int r=0; r<Size; r++)
{
if (values[r].postcode==Thekey)
{
return r;
}
}
return -5;
}
}
class gym
{
String name;
int number;
int postcode;
public gym(String n, int nm, int p)
{
name=n;
number=nm;
postcode=p;
}
public members()
{
name="Edward Downing";
number="12345";
postcode=2009;
}
public void showValues()
{
System.out.println("member " + " number " + " postcode ");
}
}