Simple Java Assignment Help

Discussion in 'OT Technology' started by SPACECATAZ, Sep 13, 2009.

  1. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    We basically have to create a bunch of methods in a class of setters and getters. Now I know how to do that and all, but when you get to the part where you have to print them or access them. I'm getting slightly confused.
    This is what I have so far:

    Code:
    package assg01;
    
    public class Secret {
        private String name;
        private int age;
        private int weight;
        private double height;
        
        public Secret(){
            name = "";
            age = 0;
            weight = 0;
            height = 0;
        }
    
        public Secret(String name, int age, int weight, double height){
            this.name = name;
            this.age = age;
            this.weight = weight;
            this.height = height;
        }
        
        public void setName(String name){
            this.name = name;
        }
        
        public String getName(){
            return name;
        }
        
        public void setAge(int age){
            this.age = age;
        }
        
        public int getAge(){
            return age;
        }
        
        public void setWeight(int weight){
            this.weight  = weight;
        }
        
        public int getWeight(){
            return weight;
        }
        
        public void setHeight(double height){
            this.height = height;
        }
    
        public double getHeight(){
            return height;
        }
        
        public void print(){
            System.out.println("This is name: "+name);
            System.out.println("This is the age: "+age);
            System.out.println("This is the weight: "+weight);
            System.out.println("This is the height: "+height);
        }
    }
    
    and this is my main:

    Code:
    package assg01;
    
    
    public class testMain {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Secret a = new Secret();
            a.Secret(John, 20, 200, 186.5);
            a.print();
        }
     
    }
    
    I'm not sure why he had us use getters and setters when he also had us use the constructors as well. Maybe for redundancy?
    This is what my print method is supposed to do:
    But it's not doing that. :squint:
     
  2. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    and I get these errors when I compile it:

     
  3. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    well the getters and setters are used AFTER the intial construction of the object.. lets say you want to change the name.. you would call the set method to change the name for that object then the get method to show the change.
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    also in your method call for the constructor, you need to put quotations around your string variables.. "John" unless John is suppose to be an enumerator.


    Code:
    public static void main(String[] args) {
    Secret a = new Secret("John", 20, 200, 186.5);
    a.print();
    }
    
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,566
    Likes Received:
    622
    First, you can't call a constructor directly:

    Code:
    public static void main(String[] args) {
            Secret a = new Secret();
    [B]        a.Secret(John, 20, 200, 186.5);[/B]
            a.print();
    }
    Like ge0 did but didn't say, combine those two lines into one, and like he did say, put quotes around your string argument:

    Code:
    public static void main(String[] args) {
            Secret a = new Secret("John", 20, 200, 186.5);
            a.print();
    }
    As for how to use the get and set methods, you might want to do some I/O.

    I'd do 3 objects.

    I'd set one of them with the long constructor (already done) at the same time you declare it:
    Code:
    Secret a = new Secret("John",20,200,186.5);
    Then I'd prompt the user for values:
    and use those values to initiate a second object:
    Code:
    Secret b = new Secret(name,age,weight,height);
    Then prompt for values again, and use the set methods:
    Code:
    Secret c = new Secret();
    c.setName(name);
    c.setAge(age);
    etc.

    And again, I'd access the values in each object different ways. Use the print() method for one, and the getXxxx() methods for the others.

    But that's just me.
     
  6. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    Thank you guys. I finally figured that it only took just to call my getMethods for the print function to work properly. :bowdown:

    Code:
    public void print() {
            System.out.println("This is name: " + getName());
            System.out.println("This is the age: " + getAge());
            System.out.println("This is the weight: " + getWeight());
            System.out.println("This is the height: " + getHeight());
            
        }
    
     
  7. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    basically the rule of OOP is to access your members through the getters and setters as much as possible.
     
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,566
    Likes Received:
    622
    rather than constructors?
     
  9. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    well you construct the intial object with the constructor.. you modify the object through the setters and read members through the getters

    sorry if I confused.
     

Share This Page