Click to See Complete Forum and Search --> : Java - I am new in to it and need some help


spacemoc
08-23-2009, 03:29 PM
This is my task

A program is to be written to store Gym registration details. A Gym member record consists of membership number, name, postcode and fee.

Write a class definition to represent such Gym membership details.

Write 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.

Write a method to display to the screen the names and membership numbers of both members.

Example data entry ( “Z123456”, “Paul Jones”, “HN78HG”, 250);


And that's what I have got but doesn't work:

import javax.swing.JOptionPane;

class Objects_arrays

{
int mem_num;
String name;
char postcode;
double fee;

{
int mem_num = JOptionPane.showInputDialog("Enter membership number");
String name = JOptionPane.showInputDialog("Enter the name of new member");
char postcode = JOptionPane.showInputDialog("What is the postcode of the new member?");
int fee = JOptionPane.showInputDialog("Enter the fee of new member");
}

public static void Member1 (mem_num, name, postcode, fee);
{
Mem_num mem_num1 = new mem_num = 15215;
Name name1 = new name = Michael;
postcode postcode1 = new postcode = NW18HP;
fee fee1 = new fee = 250;
}

System.out.printf("Membership datails:" + Member1);


public static void main(String[] args)

{
int [] gym_mem;
Weight kg = new int [78];
Height ft = new int [6];

System.out.printf("Member's Name is:" + mem_num1.getMem_num());
System.out.printf("Member's Mem Num is:" + name1.getName());

System.exit(0);

}
}

Please help

triassic
08-24-2009, 05:43 PM
Firstly, why is the class called Objects_arrays? I'd call it Gym. Also, you need a constructor method to initialize your data. You can either pass values into it, or just make it set everything to 0 and "". That's kind of what your member1 method is doing, but you need types on your parameters, and it has to be called just "public <classname>()".

Your method with the input dialogs has no name, it's just brackets. Also, you don't need to declare the types of those variables again. mem_num = JOptionPane... is all you need to do. However, the JOptionPane will return a String, so you'll need to parse it to an int. (see parseInt in the Integer class, same for the double value, parseDouble in the Double class) As for postcode I'd just make that a String.

In your main method you need to create instances (objects) of the class. Do this with the "new" command. ex. Gym gymObj = new Gym(). Or, Gym gymObj2 = new Gym(“Z123456”, “Paul Jones”, “HN78HG”, 250), if you put parameters in your constructor.

Create a separate method for your print statements. You can call it with the objects you create in main, by doing gymObj.printStuff(); or whatever you call it. Same with the data request method.

So, basically you should have five parts inside your class:
-Variable definitions (already done)
-Constructor method
-Data request method
-Print method
-Main method