0% found this document useful (0 votes)
41 views

Lab Assignment 3

The document describes a Java program that: 1) Defines an abstract Player class with subclasses Batsman and Bowler 2) The subclasses track player statistics like runs/wickets and matches played 3) The program creates objects for a batsman and bowler, displays their stats, and calculates their averages
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lab Assignment 3

The document describes a Java program that: 1) Defines an abstract Player class with subclasses Batsman and Bowler 2) The subclasses track player statistics like runs/wickets and matches played 3) The program creates objects for a batsman and bowler, displays their stats, and calculates their averages
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Q/1) Design a class named Student that has two private data – student ID and

score. The class should contain a parameterized constructor to initialize its


data member and one method to display the information. Now write a Java
program that will use an array of Student objects to represent information
about 3 students. Your program should take input from the keyboard and
display the information of the 3 students.

SOLVE:
import java.util.Scanner;

class Student {

private int student_ID;

private int score;

Student(int std_ID, int s) {

student_ID = std_ID;

score = s;

void display() {

System.out.println("ID: " + student_ID + ", score: " + score);

public class Practise1 {

public static void main(String[] args) {


Student students[] = new Student[3];

Scanner sc = new Scanner(System.in);

int stdID, stdScore;

for (int i = 0; i < 3; i++) {

System.out.print("Enter student ID: ");

stdID = sc.nextInt();

System.out.print("Enter score: ");

stdScore = sc.nextInt();

students[i] = new Student(stdID, stdScore);

for (int i = 0; i < 3; i++) {

students[i].display();

Output:
Q2/Write a Java program to create an abstract class Animal with
abstract methods eat() and sleep(). Create subclasses Lion, Tiger, and
Deer that extend the Animal class and implement the eat() and sleep()
methods differently based on their specific behavior.

Solve:
package com.mycompany.abstractclass;

abstract class Animal

abstract void eat();

abstract void sleep();

class Lion extends Animal

void eat()
{

System.out.println("Lion eats zebra and wildbeast");

void sleep()

System.out.println("Lion sleeps for 21 hours each day");

class Tiger extends Animal

void eat()

System.out.println("Tiger eats wild boar and deer");

void sleep()

System.out.println("Tiger sleeps for 18 hours each day");

class Deer extends Animal

void eat()
{

System.out.println("Deer eats wild grass and leaves");

void sleep()

System.out.println("Deer sleeps for 4 hours each day");

public class AbstractClass {

public static void main(String[] args) {

Lion ob1 = new Lion();

ob1.eat();

ob1.sleep();

System.out.println(" ");

Tiger ob2=new Tiger();

ob2.eat();

ob2.sleep();

System.out.println(" ");

Deer ob3 = new Deer();

ob3.eat();
ob3.sleep();

Output:

Q3//

Write a Java program to create an abstract class Vehicle with abstract


methods startEngine() and stopEngine(). Create subclasses Car and
Motorcycle that extend the Vehicle class and implement the respective
methods to start and stop the engines for each vehicle type.

Solve:
package com.mycompany.abstractclass;

abstract class Vehicle


{

abstract void StartEngine();

abstract void StopEngine();

class Car extends Vehicle

void StartEngine()

System.out.println("Car Engine start");

void StopEngine()

System.out.println("Car Engine stop");

class MotoreCycle extends Vehicle

void StartEngine()

System.out.println("MotoCycle engine start");

void StopEngine()
{

System.out.println("MotoreCycle engine stop");

public class AbstractClass {

public static void main(String[] args) {

Car ob1 = new Car();

ob1.StartEngine();

ob1.StopEngine();

System.out.println(" ");

MotoreCycle ob2 = new MotoreCycle();

ob2.StartEngine();

ob2.StopEngine();

}
Output:

Q5//The parent class contains the general information about an account


and an abstract method to calculate the yearly interest. For savings
account, the interest rate is 10% and for current account the interest
rate is 6%. All the data members of the Account class are initialized
through a parameterized constructor. Your program should be able to
deposit and withdraw money from a saving account. Perform the same
operation on a current account.

Solve:

package com.mycompany.tostringmethod;

abstract class Account

int acc_num;

float balance;
Account(int acc,float initial_balance)

acc_num=acc;

balance=initial_balance;

void deposit(float amount)

balance+=amount;

System.out.println("Ater deposit current balance is: "+balance);

void withdraw(float amount)

balance-=amount;

System.out.println("Ater withdraw current balance is: "+balance);

abstract void cal_interest();

class Savings_account extends Account

Savings_account(int acc,float initial_balance)

super(acc,initial_balance) ;
}

void cal_interest()

balance=(balance*10)/100;

System.out.println("with interest current balance is: "+balance);

class Current_account extends Account

Current_account(int acc,float initial_balance)

super(acc,initial_balance);

void cal_interest()

balance=(balance*6)/10;

System.out.println(" interest amount is: "+balance);

public class TostringMethod

public static void main(String[] args)


{

Savings_account ob1 =new Savings_account(1234,50000.0f);

System.out.println("Savings Account information: ");

ob1.deposit(10000.0f);

ob1.withdraw(5000.0f);

ob1.cal_interest();

Current_account ob2 = new Current_account(1567,60000.0f);

System.out.println("Current Account information: ");

ob1.deposit(15000.0f);

ob1.withdraw(3000.0f);

ob1.cal_interest();

Output:
Q5//Implement an abstract class player and two subclasses named batsman
and bowler. Each player has a name, contact address, telephone number and
status (either batsman or bowler). The batsman class maintains the total run
obtained by a batsman and the number of one day matches he participated.
Similarly, the bowler class maintains the total wickets taken by a player and the
total number of matches. The parent class contains an abstract method to
calculate the average of each player. Implement the above classes in Java.
Provide constructors to initialize the private data. Override the toString()
method in each class to display the class name. Write a program to create an
object of type batsman and bowler and calculate the average run/ wickets
obtained by a player. Your program should also call the toString() method to
display the class name

Solve:

package com.mycompany.superclass;

abstract class Player{


static final int Bowler=0;

static final int Batsman=0;

String name;

String address;

String tel;

String status;

Player(String a,String b,String c,String d)

name=a;

address=b;

tel=c;

status=d;

abstract float getEverage();

void display()

System.out.println("Player Name: "+name);

System.out.println("Address: "+address);

System.out.println("Contact No: "+tel);

System.out.println("Playing Status: "+status);

}
class batsman extends Player

private int runs;

private int matches;

batsman(String a,String b,String c,String d,int e,int f)

super(a,b,c,d) ;

runs=e;

matches=f;

float getEverage()

System.out.println("Everage runs: ");

return runs/matches;

public String tostring(){

return "Batsman";

class bowler extends Player

{
private int wickets;

private int matches;

bowler(String a,String b,String c,String d,int e,int f)

super(a,b,c,d) ;

wickets=e;

matches=f;

float getEverage()

System.out.println("Everage Wickets: ");

return wickets/matches;

public String tostring(){

return "Bowler";

public class Superclass {

public static void main(String[] args) {

batsman ob1 =new batsman("Shadab


Khan","pakistan","9356302","Batsman",1000,15);

ob1.display();
bowler ob2 =new bowler("Shahin
Afridi","pakistan","967854","Bowler",115,30);

ob2.display();

System.out.println(ob1.getEverage());

System.out.println(ob2.getEverage());

Output:

You might also like