Lab Assignment 3
Lab Assignment 3
SOLVE:
import java.util.Scanner;
class Student {
student_ID = std_ID;
score = s;
void display() {
stdID = sc.nextInt();
stdScore = sc.nextInt();
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;
void eat()
{
void sleep()
void eat()
void sleep()
void eat()
{
void sleep()
ob1.eat();
ob1.sleep();
System.out.println(" ");
ob2.eat();
ob2.sleep();
System.out.println(" ");
ob3.eat();
ob3.sleep();
Output:
Q3//
Solve:
package com.mycompany.abstractclass;
void StartEngine()
void StopEngine()
void StartEngine()
void StopEngine()
{
ob1.StartEngine();
ob1.StopEngine();
System.out.println(" ");
ob2.StartEngine();
ob2.StopEngine();
}
Output:
Solve:
package com.mycompany.tostringmethod;
int acc_num;
float balance;
Account(int acc,float initial_balance)
acc_num=acc;
balance=initial_balance;
balance+=amount;
balance-=amount;
super(acc,initial_balance) ;
}
void cal_interest()
balance=(balance*10)/100;
super(acc,initial_balance);
void cal_interest()
balance=(balance*6)/10;
ob1.deposit(10000.0f);
ob1.withdraw(5000.0f);
ob1.cal_interest();
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;
String name;
String address;
String tel;
String status;
name=a;
address=b;
tel=c;
status=d;
void display()
System.out.println("Address: "+address);
}
class batsman extends Player
super(a,b,c,d) ;
runs=e;
matches=f;
float getEverage()
return runs/matches;
return "Batsman";
{
private int wickets;
super(a,b,c,d) ;
wickets=e;
matches=f;
float getEverage()
return wickets/matches;
return "Bowler";
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: