Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Basic java question

  1. #1
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Basic java question

    Hello everybody, I'm trying to write a code about this assignment;
    --- 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 ---
    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...

    Person.java

    Java 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");
    }
    }
    }
    Adult.java

    Java 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;
    }
    }
    Child.java

    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;
    }
    }
    and my Test.java which includes main;

    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);
    
    
    
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  3. #3
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default 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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Basic java question

    Quote Originally Posted by manalinik View Post
    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?
    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?
    manalinik likes this.

  5. #5
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Basic Java question
    By fred2028 in forum New To Java
    Replies: 13
    Last Post: 09-14-2011, 02:55 PM
  2. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 06:51 PM
  3. Java basic question
    By JavaJunkie in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 08:09 PM
  4. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •