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