Results 1 to 5 of 5
Thread: Basic java question
- 12-19-2011, 10:10 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Basic java question
Hello everybody, I'm trying to write a code about this assignment;
There will be classes named adult and child which are extending from person class. Below is my code, but I couldn't figure out how can i do it, any help will be appreciated, thanks in advance...--- assignment ---
Write a program to take an input data ( name, age, height and weight) of a person. For the people that are under the age of 15 create an object of a Child and assign the input values taken from the user to the object. For people older than 15, create an instance of an Adult class and assign the values to that instance..
Hint: A child should speak Turkish, and an adult should speak English. We are assuming that child runs fast however adult runs slowly.
Your program should take the data as an input from the user. Should assign the values to the classes and then should retrieve the data from classes to print the age, name, weight and height information of the child and adult.
--- assignment ---
Person.java
Adult.javaJava Code:public class Person { protected String name; protected int age, height, weight; public Person() { this.name=""; this.age=0; this.height=0; this.weight=0; } public String toSpeak() { if (age<15) { return "Turkish"; } else { return "English"; } } public void run() { if (age<15) { System.out.println("runs faster"); } else { System.out.println("runs slower"); } } }
Child.javaJava Code:public class Adult extends Person { public Adult() { super(); } public Adult(String name, int age, int height, int weight) { this.name=name; this.age=age; this.height=height; this.weight=weight; } public String getName() { return this.name; } public int getAge() { return this.age; } public int getWeight() { return this.weight; } }
and my Test.java which includes main;Java Code:public class Child extends Person { public Child() { super(); } public Child(String name, int age, int height, int weight) { this.name=name; this.age=age; this.height=height; this.weight=weight; } public String getName() { return this.name; } public int getAge() { return this.age; } public int getWeight() { return this.weight; } }
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test{ public static void main(String[] args) { String name="", ageStr="", heightStr="", weightStr=""; Person abc= new Person(); JFrame table = new JFrame(); table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); table.setSize(400, 300); table.setVisible(true); JPanel tableInside = new JPanel(); tableInside.setLayout(new GridLayout(5,2)); JLabel nameLbl = new JLabel("Name: "); JTextField nameTf = new JTextField(); JLabel ageLbl = new JLabel("Age: "); JTextField ageTf = new JTextField(); JLabel heightLbl = new JLabel("Height: "); JTextField heightTf = new JTextField(); JLabel weightLbl = new JLabel("Weight: "); JTextField weightTf = new JTextField(); //button JButton btn = new JButton("OK"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); tableInside.add(nameLbl); tableInside.add(nameTf); tableInside.add(ageLbl); tableInside.add(ageTf); tableInside.add(heightLbl); tableInside.add(heightTf); tableInside.add(weightLbl); tableInside.add(weightTf); tableInside.add(btn); tableInside.setVisible(true); table.add(tableInside); table.pack(); name = nameTf.getText(); ageStr=ageTf.getText(); heightStr=heightTf.getText(); weightStr=weightTf.getText(); abc.name=name; abc.age=Integer.parseInt(ageStr); abc.height=Integer.parseInt(heightStr); abc.weight=Integer.parseInt(weightStr); } }
-
Re: Basic java question
What is your specific question? We don't do well with "here's my code, please fix it" type questions, and much better with "I'm getting an error message on line xxx" type questions. Bet of luck.
- 12-19-2011, 11:14 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: Basic java question
So ok, i just need to ask to user what is his age from gui (i tried JTextField) and then assign this value to my age(int). What should i do?
-
Re: Basic java question
I think you're right, get the String from the JTextField that should hold the age, parse it (using Integer.parseInt(...)), and then create your Person object with the data extracted from this number and the data held in the other fields.
Why not show us your attempt to do this and let us know of any problems with this attempt?
- 12-27-2011, 02:06 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Basic Java question
By fred2028 in forum New To JavaReplies: 13Last Post: 09-14-2011, 02:55 PM -
Basic JSP question..
By casid in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 12-28-2009, 06:51 PM -
Java basic question
By JavaJunkie in forum New To JavaReplies: 3Last Post: 04-17-2009, 08:09 PM -
Basic question on OOP.
By madthinker in forum New To JavaReplies: 7Last Post: 01-26-2009, 07:09 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks