0% found this document useful (0 votes)
26 views5 pages

Final Lab Exam

The document contains questions from a final lab exam for OOP class. It includes three programming questions involving inheritance, interfaces, and converting between measurement units. The first question involves creating a triangle class with inheritance. The second deals with interfaces and creating basic and premium music player classes. The third converts between miles and kilometers using a GUI.

Uploaded by

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

Final Lab Exam

The document contains questions from a final lab exam for OOP class. It includes three programming questions involving inheritance, interfaces, and converting between measurement units. The first question involves creating a triangle class with inheritance. The second deals with interfaces and creating basic and premium music player classes. The third converts between miles and kilometers using a GUI.

Uploaded by

Umar Mian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Final lab exam

Name : Muhammad umar waseem

Class : BSSE 2nd

Roll no : Fsd-sp-179

Subject : OOP

QUESTION 1

package displyarea;

class GeometricShape {
String shapeName;
public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
}

class Trigon extends GeometricShape {


double base, perpendicular, hypotenuse;

public Trigon() {
base = 1.0;
perpendicular = 1.0;
hypotenuse = 1.0;
}
public Trigon (double base,double perpendicular,double hypotenuse){
this.base = base;
this.perpendicular = perpendicular;
this.hypotenuse = hypotenuse;
}

public void setBase(double base) {


this.base = base;
}
public void setPerpendicular(double perpendicular) {
this.perpendicular = perpendicular;
}

public void setHypotenuse(double hypotenuse) {


this.hypotenuse = hypotenuse;
}
public double getBase() {
return base;
}

public double getPerpendicular() {


return perpendicular;
}

public double getHypotenuse() {


return hypotenuse;
}

public void displayArea() {


double area = 0.5 * (perpendicular * base);
System.out.println("Area of the triangle: " + area);
}
}

public class main {


public static void main(String[] args) {
Trigon trigon1 = new Trigon(3.0, 4.0, 5.0);
trigon1.setShapeName("triangle");
trigon1.displayArea();

Trigon trigon2 = new Trigon(5.0, 12.0, 13.0);


trigon2.setShapeName("triangle");
trigon2.displayArea();

Trigon trigon3 = new Trigon(7.0, 24.0, 25.0);


trigon3.setShapeName("triangle");
trigon3.displayArea();
}
}
Question no.2
package musicplaylist;

public interface music {

void play();
void pause();
void stop();
}
interface PremiumMusic extends Music {
void download();
void createPlaylist();
void sharePlaylist();
}
class BasicPlayer implements Music {
@Override
public void play() {
System.out.println("Music is playing");
}

@Override
public void pause() {
System.out.println("Music is paused");
}

@Override
public void stop() {
System.out.println("Music is stopped");
}
}

// PremiumPlayer class implementing PremiumMusic interface


class PremiumPlayer implements PremiumMusic {
@Override
public void play() {
System.out.println("Music is playing");
}

@Override
public void pause() {
System.out.println("Music is paused");
}

@Override
public void stop() {
System.out.println("Music is stopped");
}

@Override
public void download() {
System.out.println("Downloading music");
}

@Override
public void createPlaylist() {
System.out.println("Creating playlist");
}

@Override
public void sharePlaylist() {
System.out.println("Sharing playlist");
}
}

public class TestMusic {


public static void main(String[] args) {
BasicPlayer basicPlayer = new BasicPlayer();
PremiumPlayer premiumPlayer = new PremiumPlayer();

basicPlayer.play();
basicPlayer.pause();
basicPlayer.stop();

premiumPlayer.play();
premiumPlayer.pause();
premiumPlayer.stop();
premiumPlayer.download();
premiumPlayer.createPlaylist();
premiumPlayer.sharePlaylist();
}
}

Question no 3
Question no 3
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MileConverter extends JFrame {


private JTextField mileField;
private JLabel resultLabel;

public MileConverter() {
super("Mile to Kilometer Converter");
JLabel mileLabel = new JLabel("Enter miles:");
mileField = new JTextField(10);
JButton convertButton = new JButton("Convert!");
resultLabel = new JLabel();
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(mileLabel);
add(mileField);
add(convertButton);
add(resultLabel);
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
convertMilesToKilometers();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setVisible(true);
}

private void convertMilesToKilometers() {


try {
double miles = Double.parseDouble(mileField.getText());
double kilometers = miles * 1.609;
resultLabel.setText(miles + " miles is equal to " + kilometers + " kilometers.");
} catch (NumberFormatException e) {
resultLabel.setText("Invalid input. Please enter a valid number.");
}
}

public static void main(String[] args) {


new MileConverter();
}
}

You might also like