Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > newbie interface question

Reply
Thread Tools

newbie interface question

 
 
happytoday
Guest
Posts: n/a
 
      08-27-2012
Dear All,
I am testing interface but really I can not apply what is explained in
oracle tutorials . Could you please explain why I have errors with
this code :

//*data of class Bicycle is cadence , gear , speed
//*methods of class Bicycle is changeCadence ,
IncreaseGearToIncreaseSpeed , IncreadSpeed , DeareaseSpeed
//*data of class Bicycle is Cadence , Gear , Speed

class prog01_BicycleDemo_inheirtance_trial01
{
public static void main (String [] argc )
{
Bicycle bike1 = new Bicycle();
MountainBicycle bike2 = new MountainBicycle();

bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();


bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();

bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();

bike2.speed=1;
bike2.cadence=1;
bike2.gear=1;
bike2.printresults();

bike2.mb_speed=1;
bike2.mb_cadence=1;
bike2.mb_gear=1;
bike2.mb_printresults();
}
}

class Bicycle
{
int speed;
int cadence;
int gear ;

void speed(int newspeed)
{
speed=newspeed;
}

void cadence(int newcadence)
{
cadence=newcadence;
}

void gear(int newgear)
{
gear=newgear;
}

void printresults()
{
System.out.println( "Cadence : " + cadence + " Gear : " + gear + "
Speed :" + speed );
}
}

class MountainBicycle extends Bicycle
{
int mb_speed;
int mb_cadence;
int mb_gear ;

void mb_speed(int newmb_speed)
{
mb_speed=newmb_speed;
}

void mb_cadence(int newmb_cadence)
{
mb_cadence=newmb_cadence;
}

void mb_gear(int newmb_gear)
{
mb_gear=newmb_gear;
}

void mb_printresults()
{
System.out.println( "MB_Cadence : " + mb_cadence + " MB_Gear : " +
mb_gear + " MB_Speed :" + mb_speed );
}
}

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-28-2012
On 8/27/2012 7:17 PM, happytoday wrote:
> Dear All,
> I am testing interface but really I can not apply what is explained in
> oracle tutorials . Could you please explain why I have errors with
> this code :
> [...]


What "errors" do you have? For me, the code compiles just
fine, and runs producing the output

Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
MB_Cadence : 1 MB_Gear : 1 MB_Speed :1

(Follow-ups set to c.l.j.help only, dropping c.l.j.programmer.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Jens Vonderheide
Guest
Posts: n/a
 
      08-29-2012
happytoday wrote:

> I am testing interface but really I can not apply what is explained in
> oracle tutorials . Could you please explain why I have errors with
> this code :


Besides the fact that your example may or may not compile, there are
some sever issues with the way to design and use classes.

For better encapsulation, do not directly access fields of an object
outside of it. You already wrote setters, but you are not using them.
Also Java best practice is to prefix getters with "get" and setters with
"set". If you look again into the Java Tutorial, you will see that the
Bicycle class is actually written that way.

So Bicycle and its usage would be

class Bicycle {
private int speed;
private int cadence;
private int gear;

public int getSpeed() { return speed; }
public void setSpeed(int speed) { this.speed = speed; }
// add other getters and setters in the same way
public void printResults() {
System.out.println("Bike: [" + speed + ", " + cadence +
", " + gear + "]");
}
}

Bicycle bike1 = new Bicycle();
bike1.setSpeed(1);
// set other data
bike1.printResults();

This way, you can later add checks to the setters (i.e. if your bike
only has 10 gears, you can handle setting it you gear 11 in an
"intelligent" way).

Also, there is no need to redeclare fields of a class that you inherit
from. Since speed, cadence and gear are already declared for Bicycle,
you do not need to redeclare them for MountainBicycle:

class MountainBicycle extends Bicycle {
public void printResults() {
System.out.println("MountainBike: [" + speed + ", " + cadence +
", " + gear + "]");
}
}
Bicycle bike2 = new MountainBicycle();
bike2.setSpeed(1);
// set other data
bike2.printResults();


BTW: Another good practice is instead of your printResults() method to
declare a method called toString() that converts the data to a String
and then use System.out.println from the caller:
class Bicycle() {
// ...
public String toString() {
return "Bike: [" + speed + ", " + cadence +
", " + gear + "]";
}
}
Bicycle bike1 = new Bicycle();
System.out.println(bike1.toString());

This allows more flexibility. For example, if you decide to write the
data to stderr instead of stdout, you only need to change the calling
code to
System.err.println(bike1.toString());

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Going from higher security level interface to lower security interface- HELP!!! - AM Cisco 4 12-28-2004 09:52 PM
PCI interface or USB interface David Wireless Networking 4 09-16-2004 01:01 PM
allow ssh only on outside interface, but telnet on inside interface of router no-one Cisco 0 07-28-2004 04:17 PM
PIX: how to allow 1 host from outside interface to access another host on the inside interface? jonnah Cisco 1 04-21-2004 02:26 PM
static nat between phisical interface and virtual interface on same ethernet Andrea Cisco 0 04-19-2004 09:37 AM



Advertisments