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 9 of 9

Thread: Inheritance problem

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Inheritance problem

    .
    Last edited by Markusovich_; June 20th, 2019 at 06:08 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Inheritance problem

    Quote Originally Posted by Markusovich_ View Post
    I keep getting 0.0 for the commission, is this a problem with the inheritance?

    package lab4;
     
    import lab4.CommissionCalculator;
     
    public class Driver {
     
    	public static void main(String[] args) {
     
    		CommissionCalculator x = new CommissionCalculator();  //Creating Commission object
    		x.Run();
    	}
     
    }


    package lab4;
     
    public class Policy {
     
    	double commission;
     
    	public Policy(){
    		commission = 0.0;
    	}
    	public void getCommission() {
    		System.out.println("Computing commission in Policy class: ");
    		commission = 0.0;
    	}
    	public void setCommission(double commission) {
    		this.commission = commission;
    	}
    	public String toString() {
    		return "Commission: " + commission + "\n";
    	}
     
    }


    package lab4;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class CommissionCalculator {
    		//Auto a;  //Defining 3 objects of each class
    		//Home b;
    		//Life c;
    		ArrayList<Policy> p = new ArrayList<Policy>();
     
    	public CommissionCalculator() {
    		//a = new Auto(null, null, null, 0, 0);  //Initializing, Constructor
    		//b = new Home(null, 0, 0, 0, 0);
    		//c = new Life(null, 0, 0);
    		p = new ArrayList<Policy>();
    	}
    	public void Run() {
    		Scanner scan = new Scanner(System.in);
    		int j=0;
    		do
    		{
    			j++;
    			System.out.println("-----------------------------");
    			System.out.println("Welcome to Parkland Insurance");
    			System.out.println("-----------------------------");
    			System.out.println("Enter any of the following:");
    			System.out.println("\t1) Enter auto insurance policy information");
    			System.out.println("\t2) Enter home insurance policy information");
    			System.out.println("\t3) Enter life insurance policy information");
    			System.out.println("\t4) Print all sales entered");
    			System.out.println("\t5) Quit");			
    			System.out.println("Select an option: ");
    			int i;
    			i = scan.nextInt();
    			if(i==1){
    				System.out.println("First name, Make, Model, Liability, and Collision (with spaces): ");
    				String insured = scan.next();
    				String make = scan.next();
    				String model = scan.next();
    				double liability = scan.nextDouble();
    				double collision = scan.nextDouble();
    				p.add(new Auto(insured,make,model,liability,collision));
    			}
    			if(i==2){
    				System.out.println("First name, House sq footage, Dwelling coverage"
    						+ ", Contents coverage, Liability coverage (with spaces): ");
    				String insured = scan.next();
    				double footage = scan.nextDouble();
    				double dwelling = scan.nextDouble();
    				double contents = scan.nextDouble();
    				double liability = scan.nextDouble();
    				p.add(new Home(insured,footage,dwelling,contents,liability));
    			}
    			if(i==3){
    				System.out.println("First name, Age, Term life coverage (with spaces): ");
    				String insured = scan.next();
    				int age = scan.nextInt();
    				double termlife = scan.nextDouble();
    				p.add(new Life(insured,age,termlife));
    				}
    			if(i==4){
    				for (int n=0; n<p.size(); n++) {
    					System.out.println(p.get(n));
    				}
    			}
    			if(i<1 || i>4){
    				System.out.println("Goodbye");
    				break;
    			}
    		}while(j<999999999);
    	}
    }


    package lab4;
     
    public class Auto extends Policy{
     
    	String insured;
    	String make;
    	String model;
    	double liability;
    	double collision;
     
    	public Auto(String insured_, String make_, String model_, double liability_, double collision_){
    		super();
    		insured = insured_;
    		make = make_;
    		model = model_;
    		liability = liability_;
    		collision = collision_;
    	}
    	public Auto(){
    		super();
    		insured = null;
    		make = null;
    		model = null;
    		liability = 0.0;
    		collision = 0.0;
    	}
    	public String getInsured() {
    		return insured;
    	}
    	public void setInsured(String insured) {
    		this.insured = insured;
    	}
    	public String getMake() {
    		return make;
    	}
    	public void setMake(String make) {
    		this.make = make;
    	}
    	public String getModel() {
    		return model;
    	}
    	public void setModel(String model) {
    		this.model = model;
    	}
    	public double getLiability() {
    		return liability;
    	}
    	public void setLiability(double liability) {
    		this.liability = liability;
    	}
    	public double getCollision() {
    		return collision;
    	}
    	public void setCollision(double collision) {
    		this.collision = collision;
    	}
    	public void getCommission(){
    		System.out.println("Computing the commission: ");
    		this.commission = (this.liability+this.collision)*1.30;
    	}
    	public String toString(){
    		String shapeString = super.toString();
    		return("AUTO INSURANCE POLICY" + "\n\t" + "Policy information includes: " 
    				+ "\n\tName: " + insured + "\n\tMake and Model: " + make + " " + model + "\n\tLiability: " + liability + "\n\tCollission: " + collision
    				+ "\n\tCommision: " + shapeString + "\n-------------------------------------------");
    	}
    	public String Error(){
    		return("Quit");
    	}
     
    }


    package lab4;
     
    import lab4.Policy;
     
    public class Home extends Policy{
     
    	String insured;
    	double footage;
    	double dwelling;
    	double contents;
    	double liability;
     
    	public Home(String insured_, double footage_, double dwelling_, double contents_, double liability_){
    		super();
    		insured = insured_;
    		footage = footage_;
    		dwelling = dwelling_;
    		contents = contents_;
    		liability = liability_;
    	}
    	public Home(){
    		super();
    		insured = null;
    		footage = 0.0;
    		dwelling = 0.0;
    		contents = 0.0;
    		liability = 0.0;
    	}
    	public String getInsured() {
    		return insured;
    	}
    	public void setInsured(String insured) {
    		this.insured = insured;
    	}
    	public double getFootage() {
    		return footage;
    	}
    	public void setFootage(double footage) {
    		this.footage = footage;
    	}
    	public double getDwelling() {
    		return dwelling;
    	}
    	public void setDwelling(double dwelling) {
    		this.dwelling = dwelling;
    	}
    	public double getContents() {
    		return contents;
    	}
    	public void setContents(double contents) {
    		this.contents = contents;
    	}
    	public double getLiability() {
    		return liability;
    	}
    	public void setLiability(double liability) {
    		this.liability = liability;
    	}
    	public void getCommission(){
    		System.out.println("Computing the commission: ");
    		this.commission = (this.liability * 1.30) + ( (this.dwelling + this.contents) * 1.20);
    	}
    	public String toString(){
    		String shapeString = super.toString();
    		return("HOME INSURANCE POLICY" + "\n\t" + "Policy information includes: " 
    				+ "\n\t" + insured + "\n\tHouse square footage: " + footage + "\n\tDwelling coverage: " 
    				+ dwelling +
    				"\n\tContents coverage: " + contents + "\n\tLiability: " + liability + "\n\tCommission: " 
    				+ shapeString
    				+ "\n-------------------------------------------");
    	}
    }


    package lab4;
     
    import lab4.Policy;
     
    public class Life extends Policy{
     
    	String insured;
    	int age;
    	double termlife;
     
    	public Life(String insured_, int age_, double termlife_) {
    		super();
    		insured = insured_;
    		age = age_;
    		termlife = termlife_;
    	}
    	public Life() {
    		super();
    		insured = null;
    		age = 0;
    		termlife = 0.0;
    	}
    	public String getInsured() {
    		return insured;
    	}
    	public void setInsured(String insured) {
    		this.insured = insured;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public double getTermlife() {
    		return termlife;
    	}
    	public void setTermlife(double termlife) {
    		this.termlife = termlife;
    	}
    	public void getCommission() {
    		System.out.println("Computing the commission: ");
    		this.commission = this.termlife * 1.20;
    	}
    	public String toString() {
    		String shapeString = super.toString();
    		return("LIFE INSURANCE POLICY" + "\n\t" + "Policy information includes: " 
    				+ "\n\t" + insured + "\n\tAge: " + age + "\n\tTerm life coverage: " + termlife 
    				+ "\n\tCommission: " + shapeString + "\n-------------------------------------------");
    	}
     
    }
    Can you copy the contents of the console window that shows the program's input and output and paste it here?
    Add some comments where there is unexpected results shown. For example: <<<<<< This should be 44.5
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance problem

    .
    Last edited by Markusovich_; June 20th, 2019 at 06:08 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Inheritance problem

    <--------------------- DISPLAYING 0.0
    Is 0.0 the correct value? What is the correct value?
    Where is the variable that is printed given a value? I see a variable named commission that is initialized to 0.0.

    Where does the String: Commission: Commission: come from in the program?
    Why is Commision: repeated?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance problem

    .
    Last edited by Markusovich_; June 20th, 2019 at 06:09 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Inheritance problem

    I don't see any code that sets the value of commission in the Policy class. Where is that done?

    Add a print statement following all the statements that compute the value of commission that prints out the value of commission.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance problem

    .
    Last edited by Markusovich_; June 20th, 2019 at 06:09 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Inheritance problem

    It's supposed to be done
    Yes, but where is it actually being done? The code must be executed to have any effect on a variable's value.
    Is the code executing?
    Prove it by adding some print statements to print out the computed values.

    I do not see this in the print out: Computing the commission:
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Inheritance problem

    @Markusovich_

    Stop editing your post !

    Any time you post something on the web, you should be prepared for it to be out there, somewhere, forever.
    If you are worry your post will be visible by public, I would suggest you not posting sensitive information to public forums.
    Last edited by John Joe; June 20th, 2019 at 09:58 PM.
    Whatever you are, be a good one

Similar Threads

  1. problem with inheritance
    By a1_shay in forum Java Theory & Questions
    Replies: 2
    Last Post: September 20th, 2013, 08:12 AM
  2. Problem with inheritance
    By barry1888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 15th, 2013, 01:49 PM
  3. Java Inheritance problem
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2013, 07:34 PM
  4. problem with inheritance
    By freedom12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 29th, 2013, 04:59 AM
  5. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM

Tags for this Thread