lab rep12
lab rep12
lab rep12
Lab Number 12
o SUBMITTED BY:
M.RAHIM JAMIL
DEGREE 45 CE SYN-B
Reg No. 481105
o SUBMITTED TO:
LE EMAN FATIMA
o Related Topic/Chapter in theory class:
o Hardware/Software required:
Hardware: PC
Software Tool: MICROSOFT VISUAL STUDIO 2022
TASK 1:
Write a program that creates an Array List of pets. An item in the
list is either a Dog or a Cat. For each pet, enter its name and type (‘c’
for cat and ‘d’ for dog). Stop the input when the string STOP is entered
for the name. After the input is complete, output the name and type for
each pet in the list. Modify the Dog class to include a new instance
variable weight (double) and the Cat class to include a new instance
variable coat Color (string). Add the corresponding accessors and
mutators for the new instance variables. Modify it by inputting
additional information appropriate for the type. First you input name
and type, as before. If the type is a cat, then input its coat color. If the
type is a dog, then input its weight. After the input is complete, output
the name, type, and coat color for the cats and the name, type, and
weight for the dogs.
Now you have a list of Dog and Cat objects and want to find the
average, minimum, and maximum weight of dogs. To compute
these values, you must scan the whole list. It would be more efficient
if you could get the results by traversing only Dog objects in the list.
One approach to achieve this improvement is to create another list
that includes only Dog objects (references to Dog objects.
Create the additional dog list. Then find the average, minimum,
and maximum weights of the dogs by traversing the dog list.
CODE:
class Pet {
private String name;
private char type;
3
}
class Dog extends Pet {
private double weight;
public Dog(String n, double w) {
super(n, 'd');
this.weight = w;
}
public double getWeight() {
return weight;
}
public void setWeight(double w) {
this.weight = w;
}
4
Main.java:
import java.util.Scanner;
while (true) {
System.out.print("Enter pet name or 'STOP' to finish: ");
String name = inp.nextLine();
if (name.equals("STOP")) {
break;
}
if (type == 'c')
{
System.out.print("Enter coat color: ");
String color = inp.nextLine();
pets[petCount++] = new Cat(name, color);
}
else if (type == 'd')
{
System.out.print("Enter weight: ");
double weight = inp.nextDouble();
inp.nextLine();
5
System.out.println("Pet Details:");
for (int i = 0; i < petCount; i++) {
Pet pet = pets[i];
if (pet.getType() == 'c') {
Cat cat = (Cat) pet;
System.out.println("Cat: Name ="+ cat.getName()+ "\nCoat
Color = "+ cat.getcolor());
}
else if (pet.getType() == 'd') {
Dog dog = (Dog) pet;
System.out.println("Dog: Name = "+dog.getName()+"\nWeight
="+ dog.getWeight());
}
}
if (dogCount > 0) {
double totalWeight = 0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
OUTPUT:
TASK 2:
Design a class Circle capable of representing circle of
any radius. Your class should provide default, parameterized
constructor, setter, getter, calcArea, calcCircum. Your setter
should take input from user, and throws an exception if user
enters negative value in radius. Handle the exception and
ask user to re-enter valid value.
CODE:
import java.util.Scanner;
7
class Circle {
private double radius;
public Circle() {
this.radius = 0;
}
public Circle(double r) {
if (r < 0) {
throw new IllegalArgumentException("Radius cannot be
negative.");
}
this.radius = r;
}
public double getr() {
return radius;
}
public void setr() {
Scanner inp = new Scanner(System.in);
while (true) {
try {
System.out.print("Enter the radius: ");
radius = inp.nextDouble();
if (radius < 0) {
throw new IllegalArgumentException("Radius cannot be
negative.");
}
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage() + " Please enter a
valid radius.");
inp.nextLine();
}
}
}
public double calcarea() {
return 3.14 * radius * radius;
}
public double calccircum() {
return 2 * 3.14 * radius;
}
}
8
Main.java:
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
c.setr();
System.out.println("Area of the circle: " + c.calcarea());
System.out.println("Circumference of the circle: " +
c.calccircum());
}
}
OUTPUT:
TASK 3:
Create a class location with integer attributes of latitude
and longitude and a Boolean attribute which represents
whether person has visited this location or not. Create a
function in class that finds distance between two points but
first checks if both the locations are marked visited. Incase
both marked not visited or any one of them marked not
visited the function should throw an exception saying
9
CODE:
public class Location {
private int latitude;
private int longitude;
private boolean visited;
Main.java:
import java.util.Scanner;
System.out.println("Location 1:");
System.out.println("Enter Latitude:");
int lat1 = scanner.nextInt();
System.out.println("Enter longitude:");
int lon1 = scanner.nextInt();
System.out.println("Has Location 1 been visited?:");
boolean v1 = scanner.nextBoolean();
Location l1 = new Location(lat1, lon1, v1);
System.out.println("Location 2:");
System.out.println("Enter latitude:");
int lat2 = scanner.nextInt();
System.out.println("Enter longitude:");
int lon2 = scanner.nextInt();
System.out.println("Has Location 2 been visited?:");
boolean v2 = scanner.nextBoolean();
Location loc2 = new Location(lat2, lon2, v2);
try {
double distance = l1.calcd(loc2);
System.out.printf("Distance between locations: "+ distance);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
scanner.close();
}
}
1
1
OUTPUT:
TASK 4:
Design a class that keeps track of a student’s food
purchases at the campus cafeteria. A meal card is assigned
to an individual student. When a meal card is first issued, the
balance is set to the number of points. If the student does
not specify the number of points, then the initial balance is
set to 100 points. Points assigned to each food item are a
whole number. A student can purchase additional points at
any time during a semester. Every time food items are
bought; points are deducted from the balance. If the balance
becomes negative, the purchase of food items is not allowed,
and an exception should be thrown displaying a message
indicating remaining balance. There is obviously more than
one way to implement the MealCard class. Any design that
supports the key functionalities is acceptable. Put this class
in the myutil package. Draw UML class diagram of your
design and submit it along with your lab task.
1
2
CODE:
public class MealCard {
private int balance;
private String student;
public MealCard(String s) {
this.student = s;
this.balance = 100;
}
public MealCard(String s, int i) {
this.student = s;
this.balance = i;
}
public void purchase(int points) throws IllegalArgumentException {
if (points > balance) {
throw new IllegalArgumentException("Insufficient points.
Remaining balance: " + balance);
}
balance -= points;
}
public void addPoints (int points)
{
balance += points;
}
public int getBalance ()
{
return balance;
}
@Override
public String toString ()
{
return "Student: " + student + ", Balance: " + balance + "
points";
}
}
Main.java:
1
3
OUTPUT: