This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class

 
Babar Atta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi(no need to read my abstract class if you do not have time.)
i have problem with the my abstract class. i do not know how to call my abstract class in main class.
here are the codes of my main class and abstract class. this main calss work fine with the simple class but it is not woking with abstract class. i just want to know how can i access my abstract . does anybody has some solution of my problem.
import javax.swing.*;
class MainVehicle
{
public static void main(String[] args)
{
String name;
double speed;
String direction;
int stop;

Vehicle[] car= new Vehicle[2];;
for(int i=0;i<2;i++)
{
/
car= new Vehicle();
// Enter the name of owner of car.
name=JOptionPane.showInputDialog("Enter the Owner's Name");
car.setName(name);
String Choose=JOptionPane.showInputDialog("Which car you do like: SUV or Car:");
car.Seats(Choose);
//Enter the speed of the car
String input1=JOptionPane.showInputDialog("Enter the Speed of car");
speed=Double.parseDouble(input1);
//calling method from calss Vehicle for speed change
car.ChangeSpeed(speed);
System.out.println(car.toString());
// you will get car owner name id of car and speed and direction of the car
String input2=JOptionPane.showInputDialog("Enter the angle or (right/leftor
90/-90) to turn car");//Enter the direction of car
if(input2.equals("90") || input2.equals("-90"))
{
int dir=Integer.parseInt(input2);
car.Turn(dir);
// call the method Turn for changing direction
}
else //method overloading
{
direction=input2.toString();
//call the method Turn for changing direction
car.Turn(direction);
}
System.out.println("New Direction of the car is "+car.getDirection());
String input3=JOptionPane.showInputDialog("If you want to stop the car enter 0
or enter speed to increase or decrease:");
stop=Integer.parseInt(input3);
car.Stop(stop);
System.out.println("The speed of the car is "+car.getSpeed()+"per miles");
// print speed of the car after stoping or changing speed
}
System.exit(0);
}
}
abstract class Vehicle
{
private double speed=0;
private String direction="East";
private String name;
private int Id=assignId();
private static int nextId=2000;

public Vehicle(String name) //constractor with one parameter
{
this.name=name;
}
//emply constractor with no parameter
public Vehicle()
{
}
//abstract method for seat of car
public abstract void Seats(String Type);
// abstract method that give the number of seats of car
public abstract int getSeats();
// abstract method that gives the number of occupid seats of car
public abstract int getOccupidSeats(int occupid);
public void setName(String name)//this methode set the name of the vehicle owner
{
this.name=name;
}

public double getSpeed()//this method for get speed
{
return speed;
}
public String getDirection()// this method for get direction
{
return direction;
}
public String getName()// this method for get name of the vehicle owner
{
return name;
}
public int getId()// this method for getting id of vechile
{
return Id;
}
public void ChangeSpeed(double change)//this method for changing speed
{
speed=speed+change;
}
public void ZeroSpeed()//this method for stoping the car
{
speed=0;
}
public String Turn(int deg)// this is Turn method and overloaded for changing
the direction of Vehicle
{
String s=Integer.toString(deg);
s=s.trim();
if(s.equals("90"))
{
direction="North";
}
if(s.equals("-90"))
{
direction="South";
}
return direction;
}
public void Turn(String dir)//this is Turn method and overloaded for changing
the direction of Vehicle
{
if(dir.equals("left"))
{
direction="North";
}
if(dir.equals("right"))
{
direction="South";
}
}
public void Stop(int stop)// this is stop vehicle method
{
if(stop==0)
{
speed=0;
System.out.println("You stoped your car: ");
}
else
{
speed=stop+speed;
System.out.println("Your care is still running with the speed of "+speed+
"miles/h");
}
}

static int assignId()// this is static assigned id method
{
int r=nextId;
nextId++;
return r;
}
public String toString()
{
return "Your name is : "+getName()
+"Your Car id is : "+getId()
+"Your Car speed is : "+getSpeed()
+"Your direction is : "+getDirection();
}
}
class PsssengerVehicle extends Vehicle
{
private int NumberOfSeats;
private int OccupiedSeats;
public PsssengerVehicle(String name)
{
super(name);
}
public void Seats(String Type)
{
if(Type.equalsIgnoreCase("Car"))
NumberOfSeats=4;
else
NumberOfSeats=8;
}
public int getSeats()
{
return NumberOfSeats;
}
public int getOccupidSeats(int occupid)
{
return (getSeats()-occupid);
}
}
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Babar,
You are trying to constuct an object from an abstract class and refer it to an array. You can only call new on concrete classes and then set it to a reference of the proper type. If you change the call car = new Vehicle() to car[i] = new PsssengerVehicle(), then it should work.
Hope this helps,
Michael Morris
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic