Results 1 to 20 of 20
  1. #1
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Extending class with an array

    Need example of a class that extends another class by adding an array. Here is my code.

    public class Bike {
    int diameter;
    Bike() {
    diameter = 28;
    }
    }
    public class Gears extends Bike {
    int[] ratio;
    public Gears() {
    ratio = new int[1];
    ratio[0] = 10;
    }
    }
    public class Demo {

    public static void main(String[] args) {
    // the gears line generates a null pointer, bike line works fine
    Gears g1 = new Gears;
    Bike b1 = new Bike();
    }

    }

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

    Default Re: Extending class with an array

    Quote Originally Posted by hitchhiker1942 View Post
    Need example of a class that extends another class by adding an array.
    This statement doesn't make much sense. Can you elaborate on just exactly what it is you need or are trying to do?

    Here is my code.
    Java Code:
    public class Bike {
    	int diameter;
    	Bike() {
    		diameter = 28;
    	}
    }
    public class Gears extends Bike {
    	int[] ratio;
    	public Gears() {
    		ratio = new int[1];
    		ratio[0] = 10; 
    	}
    }
    public class Demo {
    
    	public static void main(String[] args) {
    // the gears line generates a null pointer, bike line works fine
    		Gears g1 = new Gears;
    		Bike b1 = new Bike();
    	}
    
    }
    And what is the purpose of this code? It doesn't even compile for me.

    Also, even if fixed so that this compiled and ran, none of the lines above would cause a NPE to be thrown. Again, please clarify if we can have a slim chance of being able to help you.
    Last edited by Fubarable; 06-19-2012 at 03:40 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Extending class with an array

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    And don't post in sections marked "Attention: Only forum staff can post to these forums!"

    Moved from Java Example -> Arrays

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Extending class with an array

    Must be a case of an IDE ignoring compiler errors and executing anyway.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Extending class with an array

    Quote Originally Posted by Norm View Post
    Must be a case of an IDE ignoring compiler errors and executing anyway.
    No, I don't think so. That would throw something like a non-compilable code exception not a NPE.

  6. #6
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Extending class with an array

    You lost () to make a constructor .


    Java Code:
    
    Gears g1 = new Gears;
    --->
    Gears g1 = new Gears();

    Ksharp

  7. #7
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Extending class with an array

    Sorry for posting in the wrong place. If the MOD will move it and tell me where to go would be fine.

    The fragment is from a self-assignment to calculate the speed of various bicycles. The inputs are diameter of the drive wheel, the cadence and produces speed in mile per hour. The Mountain bike adds gear ratios. The road bike, yet to implement, is just a variation on the mountain bike. I am an old programmer trying to learn a few new tricks.

    The IDE is Eclipse Juno which fragments the code across sub-files How do I post that without editing it? Also how do I get the pretty print out? BTW that's what happen to the (), thanks KSharp!!

    What I got out of-your replies is the code is good, look else where.
    Thanks!!
    Bruce...
    Last edited by hitchhiker1942; 06-19-2012 at 04:37 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Extending class with an array

    Please post the full text of the error message and the code that caused it.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Extending class with an array

    The logic of the code still escapes me. Foremost is that I don't think that Gears should extend Bike, as it doesn't pass the "is-a" test. Think of a child class as being a more specific type of the parent class. So for example, it is perfectly logical for Canine to extend Animal and for both Dog and Wolf to extend Canine, since a canine "is-a" animal, and a dog "is-a" canine, but it would make no sense to have Tail extend Dog.

    Likewise MountainBike can extend Bike and so can TouringBike extend Bike, but it would make no sense for Gears to extend Bike since Gears is-not-a Bike. Instead they would satisfy the "has-a" or composition relationship. For instance Canine "has-a" Tail, so it would make sense for the Canine class to hold a Tail variable (and thus both Dog and Wolf would gain this through inheritance). Likewise Gears satisfies the "has-a" relationship with Bike, and so the Bike class could conceivably hold a Gears object.

    Clear as Mudd?

  10. #10
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Extending class with an array

    java code:

    Java Code:
    package Bicycles;
    
    public class Bicycle {
    	int diameter; //wheel diameter in inches
    	int cadence;  //pedel revolutions per minute
    	double speed; //miles per hour
    
    	public Bicycle() { //Constructor
    		diameter = 28;
    		cadence = 60;
    		speed = 5.0;
    	}
    	
    	public int getDiameter() {
    		return diameter;
    	}
    
    	public void setDiameter(int diameter) {
    		this.diameter = diameter;
    		getSpeed();
    		}
    	
    	public int getCadence() {
    		return cadence;
    	}
    	
    	public void setCadence(int cadence) {
    			this.cadence = cadence;
    			getSpeed();
    	}
    	
    	public double getSpeed() {
    		double circumference = diameter * 3.1416 ;
    		double miles = 63360 / circumference;
    		speed = 60 / miles * cadence;
    		return speed;
    	}
    	
    	public void setSpeed(double speed) {
    		this.speed = speed;
    	}
    	
    	public void printState() {
    		System.out.format("Size=%d Cadence=%d Speed=%.2f%n",
    				diameter, cadence, speed);
    
    	}
    	
    	public static void main(String[] args) {
    		int i;
    		Bicycle b1 = new Bicycle();
    		
    		for (i=0;i<10;i++) {
    			b1.setCadence(i*10);
    			b1.printState();
    		}
    	}
    }
    Java Code:
    package Bicycles;
    
    public class MountianBike extends Bicycle {
    	int gear; // index to array of ratios
    	double[] ratio;
    	
    	MountianBike() {
    		gear = 2;
    		ratio = new double[3];
    		ratio[0] = 3.0;
    		ratio[1] = 2.0;
    		ratio[2] = 1.0;
    		
    	}
    	
    	double getRatio() {
    		System.out.println(ratio[2]);
    		return gear;
    	}
    	
    	void setGear(int gear) {
    		this.gear = gear;
    		getSpeed();
    	}
    
    	int getGear() {
    		return this.gear;
    	}
    
    	public double getSpeed() {
    		double circumference = diameter * 3.1416 ;
    		double miles = 63360 / circumference;
    		speed = 60 / miles * cadence * getRatio();
    		return speed;
    	}
    	
    
    	public void printState() {
    		System.out.format("Size=%d Cadence=%d Gear=%d Speed=%.2f%n",
    				diameter, cadence, gear, speed);
    	}
    
    }
    Java Code:
    package Bicycles;
    
    public class Demo {
    
    	public static void main(String[] args) {
    		int i;
    		Bicycle b1 = new Bicycle();
    //	The next line generates a run-time null pointer error
    		MountianBike m1 = new MountianBike();
    /*
    		Exception in thread "main" java.lang.NullPointerException
    		at Bicycles.MountianBike.getRatio(MountianBike.java:17)
    		at Bicycles.MountianBike.getSpeed(MountianBike.java:33)
    		at Bicycles.Bicycle.<init>(Bicycle.java:11)
    		at Bicycles.MountianBike.<init>(MountianBike.java:7)
    		at Bicycles.Demo.main(Demo.java:9
    				*/
    
    		for (i=0;i<3;i++) {
    			b1.setCadence(i*10);
    			m1.setGear(i);
    			b1.printState();
    			m1.printState();
    		}
    	}
    
    }
    Last edited by Fubarable; 06-19-2012 at 05:24 PM. Reason: code tags added

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

    Default Re: Extending class with an array

    Moderator edit: [code] [/code] tags added.

    Please show which line causes the NPE in the MountainBike class.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Extending class with an array

    Quote Originally Posted by hitchhiker1942 View Post
    java code:

    Java Code:
    package Bicycles;
    
    public class Bicycle {
    	int diameter; //wheel diameter in inches
    	int cadence;  //pedel revolutions per minute
    	double speed; //miles per hour
    
    	public Bicycle() { //Constructor
    		diameter = 28;
    		cadence = 60;
    		speed = 5.0;
    	}
    	
    	public int getDiameter() {
    		return diameter;
    	}
    
    	public void setDiameter(int diameter) {
    		this.diameter = diameter;
    		getSpeed();
    		}
    	
    	public int getCadence() {
    		return cadence;
    	}
    	
    	public void setCadence(int cadence) {
    			this.cadence = cadence;
    			getSpeed();
    	}
    	
    	public double getSpeed() {
    		double circumference = diameter * 3.1416 ;
    		double miles = 63360 / circumference;
    		speed = 60 / miles * cadence;
    		return speed;
    	}
    	
    	public void setSpeed(double speed) {
    		this.speed = speed;
    	}
    	
    	public void printState() {
    		System.out.format("Size=%d Cadence=%d Speed=%.2f%n",
    				diameter, cadence, speed);
    
    	}
    	
    	public static void main(String[] args) {
    		int i;
    		Bicycle b1 = new Bicycle();
    		
    		for (i=0;i<10;i++) {
    			b1.setCadence(i*10);
    			b1.printState();
    		}
    	}
    }
    Java Code:
    package Bicycles;
    
    public class MountianBike extends Bicycle {
    	int gear; // index to array of ratios
    	double[] ratio;
    	
    	MountianBike() {
    		gear = 2;
    		ratio = new double[3];
    		ratio[0] = 3.0;
    		ratio[1] = 2.0;
    		ratio[2] = 1.0;
    		
    	}
    	
    	double getRatio() {
    		System.out.println(ratio[2]);
    		return gear;
    	}
    	
    	void setGear(int gear) {
    		this.gear = gear;
    		getSpeed();
    	}
    
    	int getGear() {
    		return this.gear;
    	}
    
    	public double getSpeed() {
    		double circumference = diameter * 3.1416 ;
    		double miles = 63360 / circumference;
    		speed = 60 / miles * cadence * getRatio();
    		return speed;
    	}
    	
    
    	public void printState() {
    		System.out.format("Size=%d Cadence=%d Gear=%d Speed=%.2f%n",
    				diameter, cadence, gear, speed);
    	}
    
    }
    Java Code:
    package Bicycles;
    
    public class Demo {
    
    	public static void main(String[] args) {
    		int i;
    		Bicycle b1 = new Bicycle();
    //	The next line generates a run-time null pointer error
    		MountianBike m1 = new MountianBike();
    /*
    		Exception in thread "main" java.lang.NullPointerException
    		at Bicycles.MountianBike.getRatio(MountianBike.java:17)
    		at Bicycles.MountianBike.getSpeed(MountianBike.java:33)
    		at Bicycles.Bicycle.<init>(Bicycle.java:11)
    		at Bicycles.MountianBike.<init>(MountianBike.java:7)
    		at Bicycles.Demo.main(Demo.java:9
    				*/
    
    		for (i=0;i<3;i++) {
    			b1.setCadence(i*10);
    			m1.setGear(i);
    			b1.printState();
    			m1.printState();
    		}
    	}
    
    }
    Is this code the version that the stack trace came from? There is no call to the method: getSpeed() on line 11 as shown in the trace.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Extending class with an array

    Quote Originally Posted by Fubarable View Post
    Moderator edit: [code] [/code] tags added.

    Please show which line causes the NPE in the MountainBike class.
    line 8 of demo????

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

    Default Re: Extending class with an array

    Quote Originally Posted by hitchhiker1942 View Post
    line 8 of demo????
    Again, which line in the **MountainBike** class.
    Last edited by Fubarable; 06-19-2012 at 06:01 PM.

  15. #15
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Extending class with an array

    Quote Originally Posted by Fubarable View Post
    The logic of the code still escapes me. Foremost is that I don't think that Gears should extend Bike, as it doesn't pass the "is-a" test. Think of a child class as being a more specific type of the parent class. So for example, it is perfectly logical for Canine to extend Animal and for both Dog and Wolf to extend Canine, since a canine "is-a" animal, and a dog "is-a" canine, but it would make no sense to have Tail extend Dog.

    Likewise MountainBike can extend Bike and so can TouringBike extend Bike, but it would make no sense for Gears to extend Bike since Gears is-not-a Bike. Instead they would satisfy the "has-a" or composition relationship. For instance Canine "has-a" Tail, so it would make sense for the Canine class to hold a Tail variable (and thus both Dog and Wolf would gain this through inheritance). Likewise Gears satisfies the "has-a" relationship with Bike, and so the Bike class could conceivably hold a Gears object.

    Clear as Mudd?
    Thank you, so how do I make gears array part of MountainBike????

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

    Default Re: Extending class with an array

    Quote Originally Posted by hitchhiker1942 View Post
    Thank you, so how do I make gears array part of MountainBike????
    If Gears is to be its own class, one that holds an array, you could create that class, but not have it extend Bike or MountainBike, and then give MountainBike a Gears variable.

  17. #17
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Extending class with an array

    Quote Originally Posted by Fubarable View Post
    Again, which line in the **MountainBike** class.
    Sorry, newbie. let me go see if I can narrow that down.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Extending class with an array

    That stack trace does not match the code you posted.
    ie:
    at Bicycles.MountianBike.getRatio(MountianBike.java:1 7)
    at Bicycles.MountianBike.getSpeed(MountianBike.java:3 3) -- Here, this implies your Bicycle constructor calls getSpeed(), whereas the code you posted does not.
    at Bicycles.Bicycle.<init>(Bicycle.java:11)
    at Bicycles.MountianBike.<init>(MountianBike.java:7)

    This would explain the exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Extending class with an array

    I noticed there is a main() in your Bicycle Class . Since your program entry is the main() of Demo Class, it is redundant I think.
    The code is good without ERROR.

    run:
    1.0
    Size=28 Cadence=0 Speed=0.00
    Size=28 Cadence=60 Gear=0 Speed=0.00
    1.0
    Size=28 Cadence=10 Speed=0.83
    Size=28 Cadence=60 Gear=1 Speed=5.00
    1.0
    Size=28 Cadence=20 Speed=1.67
    Size=28 Cadence=60 Gear=2 Speed=10.00
    成功生成(总时间:8 秒)



    Ksharp

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Extending class with an array

    You can have a main() method in all your classes. I use them for testing a class.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. class extending combining with references
    By Isty in forum New To Java
    Replies: 2
    Last Post: 11-22-2011, 12:40 AM
  2. Replies: 1
    Last Post: 12-17-2010, 10:24 AM
  3. Replies: 18
    Last Post: 11-22-2008, 06:25 PM
  4. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 10:41 PM

Tags for this Thread

Posting Permissions

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