1)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package abs;
/**
*
* @author SP23-BCS-065
*/
public abstract class Abs {
int NoofLines;
String colourOfpen;
String fillColour;
abstract void Draw();
}
/**
*
* @author SP23-BCS-065
*/
public class Circle extends Abs {
1|Page
@Override
public void Draw() {
System.out.println("this is a circle ");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SP23-BCS-065
*/
public class square extends Abs {
@Override
public void Draw()
{
System.out.println("this is a square");
}
2|Page
public class Triangle extends Abs {
@Override
public void Draw(){
System.out.println("this is a triangle");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SP23-BCS-065
*/
public class test {
public static void main(String[] args) {
Abs a =new Circle();
a.Draw();
Abs b =new Triangle();
b.Draw();
3|Page
Abs c=new square();
c.Draw();
}
}
2)
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package pkgpackage;
/**
*
* @author UZAIR MALIK
*/
/**
* Class representing a generic Package.
*/
public class Package {
String name;
String senderAddress;
String receiverAddress;
int weight;
int costPerOunce;
4|Page
public Package(String name, String senderAddress, String receiverAddress, int weight, int
costPerOunce) {
this.name = name;
this.senderAddress = senderAddress;
this.receiverAddress = receiverAddress;
this.weight = weight;
this.costPerOunce = costPerOunce;
}
double calculateCost() {
return weight * costPerOunce;
}
void display() {
System.out.println("Package Name: " + name);
System.out.println("Sender Address: " + senderAddress);
System.out.println("Receiver Address: " + receiverAddress);
System.out.println("Weight: " + weight + " ounces");
System.out.println("Cost per Ounce: $" + costPerOunce);
System.out.println("Total Cost: $" + calculateCost());
}
public class TwoDayPackage extends Package {
private double flatFee;
public TwoDayPackage(String name, String senderAddress, String receiverAddress, int weight, int
costPerOunce, double flatFee) {
super(name, senderAddress, receiverAddress, weight, costPerOunce);
this.flatFee = flatFee;
}
5|Page
@Override
double calculateCost() {
return super.calculateCost() + flatFee;
}
@Override
void display() {
super.display();
System.out.println("Flat Fee: $" + flatFee);
System.out.println("Total Cost with Flat Fee: $" + calculateCost());
}
}
/**
* Class representing an overnight package, inheriting from Package.
*/
public class OvernightPackage extends Package {
private double additionalFeePerOunce;
public OvernightPackage(String name, String senderAddress, String receiverAddress, int weight,
int costPerOunce, double additionalFeePerOunce) {
super(name, senderAddress, receiverAddress, weight, costPerOunce);
this.additionalFeePerOunce = additionalFeePerOunce;
}
@Override
double calculateCost() {
return super.calculateCost() + (weight * additionalFeePerOunce);
}
@Override
void display() {
6|Page
super.display();
System.out.println("Additional Fee per Ounce: $" + additionalFeePerOunce);
System.out.println("Total Cost with Additional Fee: $" + calculateCost());
}
}
/**
* Main class to test the package functionalities.
*/
public class Test {
public static void main(String[] args) {
Package p1 = new Package("Standard Package", "123 Safdar road", “haripur", 10, 5);
p1.display();
System.out.println();
TwoDayPackage p2 = new TwoDayPackage("Two Day Package", "123 Sender St", "456
Receiver Rd", 10, 5, 10.0);
p2.display();
System.out.println();
OvernightPackage p3 = new OvernightPackage("Overnight Package", "123 Sender St", "456
Receiver Rd", 10, 5, 2.0);
p3.display();
}
}
3..
7|Page
package abs;
public abstract class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract Boolean isOutstanding();
}
package abs;
public class Student extends Person {
private double CGPA;
public double getCGPA() {
return CGPA;
}
public void setCGPA(double CGPA) {
this.CGPA = CGPA;
}
@Override
public Boolean isOutstanding() {
return CGPA > 3.5;
}
8|Page
}package abs;
public class Professor extends Person {
private int numberOfPublications;
public int getNumberOfPublications() {
return numberOfPublications;
}
public void setNumberOfPublications(int numberOfPublications) {
this.numberOfPublications = numberOfPublications;
}
@Override
public Boolean isOutstanding() {
return numberOfPublications > 50;
}
}
package abs;
public class MainClass {
public static void main(String[] args) {
Person[] people = new Person[2];
Student student = new Student();
student.setName("Uzair MAlik");
student.setCGPA(3.6);
Professor professor = new Professor();
professor.setName("Dr. Kashif");
professor.setNumberOfPublications(100);
9|Page
people[0] = student;
people[1] = professor;
for (Person person : people) {
System.out.println(person.getName() + " is outstanding: " + person.isOutstanding());
}
}
}
4
package conversions;
public abstract class Convert {
protected double val1;
protected double val2;
public Convert(double val1) {
this.val1 = val1;
}
public double getVal1() {
return val1;
}
public double getVal2() {
return val2;
}
public abstract void compute();
}
package conversions;
10 | P a g e
public class LToG extends Convert {
public LToG(double val1) {
super(val1);
}
@Override
public void compute() {
val2 = val1 * 0.264172;
}
}
package conversions;
public class FToC extends Convert {
public FToC(double val1) {
super(val1);
}
@Override
public void compute() {
val2 = (val1 - 32) * 5 / 9;
}
}
package conversions;
public class FToM extends Convert {
public FToM(double val1) {
super(val1);
}
@Override
public void compute() {
11 | P a g e
val2 = val1 * 0.3048;
}
}
package conversions;
public class MainClass {
public static void main(String[] args) {
Convert[] conversions = new Convert[3];
conversions[0] = new LToG(10.0);
conversions[1] = new FToC(98.6);
conversions[2] = new FToM(3.0);
for (Convert conversion : conversions) {
conversion.compute();
System.out.println("Initial value: " + conversion.getVal1() + " -> Converted value: " +
conversion.getVal2());
}
}
}
12 | P a g e