55.3k views
0 votes
a) Write out an abstract class to represent a ball. Include data fields to store the weight of the ball as a double value and the color of the ball as a String. Include a no-arg constructor as well as a two-arg constructor to set these two fields. Declare an abstract method, howToPlay(), which is to describe how the ball is used by printing this description to the command line.

User Mmdanziger
by
5.4k points

2 Answers

2 votes

Answer:

See explaination

Step-by-step explanation:

Code

abstract class Ball

{

double weight;

String color;

Ball()

{

this.weight=0;

this.color=" ";

System.out.println("Abstract class constructor");

}

Ball(double weight,String color)

{

this.weight=weight;

this.color=color;

System.out.println("Parameterized constructor of Abstract class with weight = "+weight+" color = "+color);

}

public abstract void howToPlay();

}

public class Baseball extends Ball {

int no;

String name;

int bat,hi ts;

Baseball() {

super(20,"red");

System.out.println("BaseBall class Constructor");

}

Baseball(int no,String name,int bat,int hi ts)

{

this.no=no;

this.name=name;

this.bat=bat;

this.hi ts=hi ts;

}

public void howToPlay()

{

System.out.println("How to play method weight = "+weight+ " color = "+color);

}

public void display()

{

System.out.println("Player no: "+no+" name = "+name+" numberAtbat = "+bat+" hi ts = "+hit s);

}

public static void main(String[] args) {

Baseball ob j = new Baseball();

Baseball ob j1 = new Baseball(1,"abc",1,3);

ob j.howToPlay();

ob j1.display();

}

}

User Jogojapan
by
5.1k points
1 vote

Answer:

Check the explanation

Step-by-step explanation:

//Ball.java

public abstract class Ball {

double value;

String color;

public Ball() {

}

public Ball(double value, String color) {

this.value = value;

this.color = color;

}

public abstract void howToPlay();

}

////////////////////////////////////////////

//SoccerBall.java

public class SoccerBall extends Ball {

public void howToPlay() {

System.out.println("Description to how to play soccer ball");

}

}

User Brent Eicher
by
5.2k points