0% found this document useful (0 votes)
6 views9 pages

Week 7 Lab Problems

The document presents six programming problems demonstrating key object-oriented programming concepts such as method overloading, method overriding, dynamic method dispatch, upcasting, downcasting, and safe downcasting. Each problem includes a Java code example that illustrates the concept, along with sample output. The problems cover a range of applications from a food delivery app to a smart campus IoT system.

Uploaded by

bezawadakumar555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Week 7 Lab Problems

The document presents six programming problems demonstrating key object-oriented programming concepts such as method overloading, method overriding, dynamic method dispatch, upcasting, downcasting, and safe downcasting. Each problem includes a Java code example that illustrates the concept, along with sample output. The problems cover a range of applications from a food delivery app to a smart campus IoT system.

Uploaded by

bezawadakumar555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROBLEM 1 — Food Delivery App (Method Overloading)

public class FoodDeliveryApp {

public void calculateDelivery(double distance) {

double cost = distance * 5;

System.out.println("Basic delivery for " + distance + " km: $" + cost);

public void calculateDelivery(double distance, double priorityFee) {

double cost = distance * 5 + priorityFee;

System.out.println("Premium delivery: $" + cost + " (including priority fee $" + priorityFee + ")");

public void calculateDelivery(double distance, int orders) {

double base = distance * 5;

double discount = orders * 2;

double total = base - discount;

System.out.println("Group delivery for " + orders + " orders: $" + total + " (discount $" + discount
+ ")");

public void calculateDelivery(double distance, double discountPercent, double freeThreshold) {

double base = distance * 5;

if (base > freeThreshold) {

System.out.println("Festival delivery: FREE (threshold $" + freeThreshold + ")");

} else {

double total = base * (1 - discountPercent/100);

System.out.println("Festival delivery: $" + total + " (discount " + discountPercent + "%)");

}
public static void main(String[] args) {

FoodDeliveryApp delivery = new FoodDeliveryApp();

delivery.calculateDelivery(10);

delivery.calculateDelivery(15, 20);

delivery.calculateDelivery(12, 3);

delivery.calculateDelivery(5, 10, 30);

Sample Output:

Basic delivery for 10.0 km: $50.0

Premium delivery: $95.0 (including priority fee $20.0)

Group delivery for 3 orders: $54.0 (discount $6.0)

Festival delivery: $22.5 (discount 10.0%)

PROBLEM 2 — Social Media Feed (Method Overriding)

class Post {

String author, content;

Post(String a, String c) { author = a; content = c; }

public void display() { System.out.println(author + ": " + content); }

class InstagramPost extends Post {

int likes; String hashtags;

InstagramPost(String a, String c, int l, String h) { super(a,c); likes=l; hashtags=h; }

@Override public void display() { System.out.println("Instagram: " + content + " #" + hashtags + " ("
+ likes + " likes)"); }

class TwitterPost extends Post {

int retweets;
TwitterPost(String a, String c, int r) { super(a,c); retweets=r; }

@Override public void display() { System.out.println("Twitter: " + content + " (" + retweets + "
retweets)"); }

class LinkedInPost extends Post {

int connections;

LinkedInPost(String a, String c, int con) { super(a,c); connections=con; }

@Override public void display() { System.out.println("LinkedIn: " + content + " - Connections: " +
connections); }

public class SocialMediaFeed {

public static void main(String[] args) {

Post[] feed = {

new InstagramPost("Alice", "Sunset pic", 120, "sunset"),

new TwitterPost("Bob", "Java rocks!", 45),

new LinkedInPost("Carol", "New Project launched", 200)

};

for(Post p : feed) p.display();

Sample Output:

Instagram: Sunset pic #sunset (120 likes)

Twitter: Java rocks! (45 retweets)

LinkedIn: New Project launched - Connections: 200

PROBLEM 3 — Gaming Character System (Dynamic Method Dispatch)

abstract class Character {

String name;

Character(String n){ name=n; }

public abstract void attack();


}

class Warrior extends Character {

Warrior(String n){ super(n); }

@Override public void attack(){ System.out.println(name + " swings sword with high defense"); }

class Mage extends Character {

Mage(String n){ super(n); }

@Override public void attack(){ System.out.println(name + " casts fireball using mana"); }

class Archer extends Character {

Archer(String n){ super(n); }

@Override public void attack(){ System.out.println(name + " shoots arrow from long range"); }

public class GamingCharacters {

public static void main(String[] args) {

Character[] army = { new Warrior("Thor"), new Mage("Gandalf"), new Archer("Legolas") };

for(Character c: army) c.attack();

Sample Output:

Thor swings sword with high defense

Gandalf casts fireball using mana

Legolas shoots arrow from long range

PROBLEM 4 — University Library System (Upcasting)

File: LibrarySystem.java

abstract class LibraryUser {


String name;

LibraryUser(String n){ name=n; }

void enterLibrary(){ System.out.println(name + " enters the library"); }

class Student extends LibraryUser {

Student(String n){ super(n); }

void borrowBook(){ System.out.println(name + " borrows a book"); }

void accessComputer(){ System.out.println(name + " uses computer"); }

class Faculty extends LibraryUser {

Faculty(String n){ super(n); }

void reserveBook(){ System.out.println(name + " reserves a book"); }

void accessDatabase(){ System.out.println(name + " accesses research database"); }

class Guest extends LibraryUser {

Guest(String n){ super(n); }

void browseBooks(){ System.out.println(name + " browses books"); }

public class LibrarySystem {

public static void main(String[] args) {

LibraryUser[] users = {

new Student("Alice"),

new Faculty("Dr. Rao"),

new Guest("Bob")

};

for(LibraryUser u: users) {

u.enterLibrary(); // upcast reference to base type


}

Sample Output:

Alice enters the library

Dr. Rao enters the library

Bob enters the library

PROBLEM 5 — Movie Streaming Platform (Downcasting)

abstract class Content {

String title;

Content(String t){ title=t; }

void play(){ System.out.println("Playing: " + title); }

class Movie extends Content {

String rating;

int duration;

Movie(String t, String r, int d){ super(t); rating=r; duration=d; }

void showSubtitles(){ System.out.println("Showing subtitles for " + title + " (" + rating + ")"); }

class TVSeries extends Content {

int seasons, episodes;

TVSeries(String t, int s, int e){ super(t); seasons=s; episodes=e; }

void nextEpisode(){ System.out.println("Next episode for " + title); }

class Documentary extends Content {

String topic;

Documentary(String t, String topic){ super(t); this.topic=topic; }


void relatedContent(){ System.out.println("Related content on " + topic); }

public class StreamingPlatform {

public static void main(String[] args) {

Content content = new Movie("Inception","PG-13",148);

content.play();

if(content instanceof Movie){

Movie m = (Movie)content;

m.showSubtitles();

Sample Output:

Playing: Inception

Showing subtitles for Inception (PG-13)

PROBLEM 6 — Smart Campus IoT System (Safe Downcasting with instanceof)

abstract class Device {

String id;

Device(String id){ this.id=id; }

void status(){ System.out.println(id + " status: OK"); }

class SmartClassroom extends Device {

SmartClassroom(String id){ super(id); }

void controlLights(){ System.out.println(id + " lights adjusted"); }

void controlAC(){ System.out.println(id + " AC set"); }

class SmartLab extends Device {


SmartLab(String id){ super(id); }

void manageEquipment(){ System.out.println(id + " equipment running"); }

class SmartLibrary extends Device {

SmartLibrary(String id){ super(id); }

void trackBooks(){ System.out.println(id + " book availability updated"); }

public class SmartCampusIoT {

public static void control(Device d){

if(d instanceof SmartClassroom){

SmartClassroom c = (SmartClassroom)d;

c.controlLights();

c.controlAC();

} else if(d instanceof SmartLab){

((SmartLab)d).manageEquipment();

} else if(d instanceof SmartLibrary){

((SmartLibrary)d).trackBooks();

} else{

d.status();

public static void main(String[] args){

Device[] devices = {

new SmartClassroom("Room101"),

new SmartLab("Lab1"),

new SmartLibrary("LibraryMain")

};

for(Device dev: devices) control(dev);


}

Sample Output:

Room101 lights adjusted

Room101 AC set

Lab1 equipment running

LibraryMain book availability updated

You might also like