Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help doing an exercise[constructors]

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Need help doing an exercise[constructors]

    The exercise is:

    In your examination folder, there is a file called Insurance.java. This class includes the following list of attributes:
    name, age and insuranceTime. Modify this code by adding three constructors:
    - the empty constructor where the class attributes are initialised as "no name" and 100 and "12 months";
    - the constructor with three parameters: Insurance(String name, int age, String time), the order of the parameters
    is important! Use these parameters to initialise the class attributes;
    - the constructor with two parameters: Insurance(String name, int age), the insurance time should be set by default
    to 12 months.
    Add a print() method with a void return type that prints to the standard output the name, age and insurance time in
    one line separated by space, such as: “no name 100 12 month”.
    Note: you can test your solution by compiling and running the existing program InsuranceDemo.java in your
    examination folder.

    I did everything as told, but when compiling InsuranceDemo I get these errors:
    ./Insurance.java:26: error: class, interface, or enum expected
            System.out.println(name, age, time);
            ^
    InsuranceDemo.java:7: error: cannot find symbol
    		one.print();
    		   ^
      symbol:   method print()
      location: variable one of type Insurance
    InsuranceDemo.java:11: error: cannot find symbol
    		two.print();
    		   ^
      symbol:   method print()
      location: variable two of type Insurance
    InsuranceDemo.java:15: error: cannot find symbol
    		three.print();
    		     ^
      symbol:   method print()
      location: variable three of type Insurance
    4 errors

    We're not supposed to touch InsuranceDemo.java in any way. So there must be something wrong with my code?

    public class Insurance
    {
    	public String name;
    	public int age;
    	public String insuranceTime;
     
    	public Insurance(){
                name = "no name";
                age= 100;
                insuranceTime= "12 months"; }
     
            public Insurance(String name, int age, String time){
                name=name;
                age=age;
                time=insuranceTime;
            }
            public Insurance(String name, int age){
                name=name;
                age=age;
     
     
            }   
            System.out.println(name, age, time);
    }

    This is the InsuranceDemo.java code that we're not supposted to touch, just in case itself might have some errors:
    public class InsuranceDemo
    {
    	public static void main(String args[])
    	{
    		System.out.println("Creating and printing object using parameterless constructor");
    		Insurance one = new Insurance();
    		one.print();
     
    		System.out.println("Creating and printing object using three parameter constructor");
    		Insurance two = new Insurance("John Smith", 45, "6 months");
    		two.print();
     
    		System.out.println("Creating and printing object using two parameter constructor");
    		Insurance three = new Insurance("Jane Smith", 40);
    		three.print();
    	}
    }

  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Need help doing an exercise[constructors]

    the problem is where your system.out.println statement lies in your first code.

    the question instructs for there to be a method of void type called print. You should replace that print statement with the print method that prints the requested data.

    I.E

    public void print(){
    //desired print output
    }

    Furthermore, you can look into the provided test class and note that objects are calling a .print() method.
    one-three.print();

  3. The Following User Says Thank You to GStateSwish For This Useful Post:

    import.Snupas (December 8th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,482
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: Need help doing an exercise[constructors]

    In Java, executable statements other than declaration/initializations and initialization blocks must be inside methods.

  5. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help doing an exercise[constructors]

    Quote Originally Posted by GStateSwish View Post
    the problem is where your system.out.println statement lies in your first code.

    the question instructs for there to be a method of void type called print. You should replace that print statement with the print method that prints the requested data.

    I.E

    public void print(){
    //desired print output
    }

    Furthermore, you can look into the provided test class and note that objects are calling a .print() method.
    one-three.print();
    Thanks for the speedy response! At least in runs now. Code now looks like this:

    public class Insurance
    {
    	public String name;
    	public int age;
    	public String insuranceTime;
     
    	public Insurance(){
                name = "no name";
                age= 100;
                insuranceTime= "12 months"; }
     
            public Insurance(String name, int age, String time){
                name=name;
                age=age;
                time=insuranceTime;
            }
            public Insurance(String name, int age){
                name=name;
                age=age;
     
     
            }   
       public void print(){
           System.out.print(name,age,time);
    }     
    }
    The output is way off. What I'm supposed to get:
    Creating and printing object using parameterless constructor
    no name 100 12 months
    Creating and printing object using three parameter constructor
    John Smith 45 6 months
    Creating and printing object using two parameter constructor
    Jane Smith 40 12 months

    What I'm getting is:
    Creating and printing object using parameterless constructor
    no nameCreating and printing object using three parameter constructor
    nullCreating and printing object using two parameter constructor
    null

    For starters, it's not recognizing the other two variables.

  6. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Need help doing an exercise[constructors]

    First, inside of your constructors, declare your variables appropriately
    i.e
    public Insurance (String name .. etc){
    this.name = name;
    }
    the way the class will determine which name to use. Should do it this way if your parameter and data field are exact.

    error at your print statement. if you are going to do it like that, separate the data with string spaces.

    System.out.println(name +" " +age " "+time)


    you could also implement accessor methods for the fields.


    you also have output format errors. Remember that the print statement sends output.
    search for how to format that on seperate lines using \n.

  7. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help doing an exercise[constructors]

    I don't think it's based on how I write the System.out.println statement? I corrected it to the way you wrote it and it still gives the same output.

    I could try to add accecors, but it didn't ask for it in the question so the results should display normally just with a System.out.println method.

    EDIT:

    I know I need to fix the formatting, but I need to get the actual answer right first. It's still giving me null

  8. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Need help doing an exercise[constructors]

    The main problem is the variables assigning to themselves.. forgot to mention that the first time.

    fix the variable assignments.

    in your second constructor you are also doing something weird

    that should be
    insuranceTime = time; not the other way around.

    Then in your print statement, you should be referencing insuranceTime, not time, if you are going to be using your data fields over accessor methods. good luck

  9. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help doing an exercise[constructors]

    Should I create new variables for each constructor then?

    Name= name;
    Age= age;

    etc. ?

    I've tried doing this.name and this.age and it didn't change a thing.

  10. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Need help doing an exercise[constructors]

    System.out.print()
    vs
    System.out.println()

    public class  Insurance
    {
    	public String name;
    	public int age;
    	public String insuranceTime;
     
    	public Insurance(){
                name = "no name";
                age= 100;
                insuranceTime= "12 months"; }
     
            public Insurance(String name, int age, String time){
                this.name=name;
                this.age=age;
                insuranceTime = time;
            }
            public Insurance(String name, int age){
                this.name=name;
                this.age=age;
                this.insuranceTime = "12 months";
     
     
            }   
       public void print(){
           System.out.println(name+" "+age+" "+insuranceTime);

  11. The Following User Says Thank You to GStateSwish For This Useful Post:

    import.Snupas (December 8th, 2013)

  12. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help doing an exercise[constructors]

    Just needed to add a few } and your solution worked. I guess I'll have to research .this

    Thanks for putting up with me GState

Similar Threads

  1. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 10:31 PM
  2. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  3. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 08:40 PM
  4. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM