Mahad Oop 2
Mahad Oop 2
SUBMITTED TO:
Miss Arubah Hussain
SUBMITTED BY:
Mahad Khan
REGISTRATION NO:
FA23-BCS-003
Question 1:
class BeerSong {
int bottle;
if (bottle<0){
this.bottle=99;
else{
this.bottle=bottle;
}
}
String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
if (n < 20) {
} else {
if (unitVal == 0) {
} else {
class q1{
public static void main(String[] args) {
b.printSong();
Question 2:
package com.mycompany.rational;
public class Rational {
private int numerator;
private int denominator;
Question 4:
package blogentry;
import java.util.Date;
import java.text.SimpleDateFormat;
public class BlogEntry {
// Instance variables
this.username = username;
this.text = text;
// Method to get the first 10 words from the text (or the entire text if
it's less than 10 words)
} else {
summary.append(words[i]).append(" ");
}
// Main method to test the BlogEntry class
entry.displayEntry();
System.out.println("\nDisplaying summary:");
System.out.println(entry.getSummary());
Question 5:
class Counter {
this.count = 0;
this.count = 0;
count++;
if (count > 0) {
count--;
} else {
return count;
@Override
}
// Override equals method to compare two Counter objects
@Override
counter1.increase();
counter1.increase();
counter1.decrease();
counter1.reset();
Question 7:
public class Temperature {
public Temperature() {
this.temperature = 0.0;
this.scale = 'C';
this.scale = 'C';
this.temperature = temperature;
this.scale = Character.toUpperCase(scale);
if (scale == 'C') {
return round(temperature);
} else {
if (scale == 'F') {
return round(temperature);
} else {
}
}
this.temperature = temperature;
this.scale = Character.toUpperCase(scale);
this.temperature = temperature;
this.scale = Character.toUpperCase(scale);
@Override
Question 8:
import java.util.Scanner;
month = 1; // January
day = 1;
year = 1000;
setDate(1, 1, year);
}
if (aDate == null) {
System.out.println("Fatal Error.");
System.exit(0);
this.month = aDate.month;
this.day = aDate.day;
this.year = aDate.year;
this.month = monthInt;
this.day = day;
this.year = year;
} else {
System.out.println("Fatal Error");
System.exit(0);
this.year = year;
} else {
System.out.println("Fatal Error");
System.exit(0);
}
public void setMonth(int monthNumber) {
this.month = monthNumber;
} else {
System.out.println("Fatal Error");
System.exit(0);
this.day = day;
} else {
System.out.println("Fatal Error");
System.exit(0);
(as int)
return day;
return year;
switch (month.toLowerCase()) {
default:
System.out.println("Fatal Error");
System.exit(0);
switch (monthInt) {
default:
System.out.println("Fatal Error");
System.exit(0);
Question 9:
public class AnimalSpecies {
public AnimalSpecies() {
this.speciesName = "Unknown";
this.population = 0;
this.growthRate = 0.0;
this.speciesName = speciesName;
this.population = population;
this.growthRate = growthRate;
}
public String getSpeciesName() {
return speciesName;
return population;
return growthRate;
this.speciesName = speciesName;
this.population = population;
this.growthRate = growthRate;
}
public boolean endangered() {
@Override
@Override
System.out.println(species1);
System.out.println(species2);
System.out.println(species3);
species1.setPopulation(3100);
species1.setGrowthRate(-1.0);
Question 10:
class AnimalSpecies {
this.speciesName = speciesName;
this.population = population;
this.growthRate = growthRate;
return speciesName;
this.speciesName = speciesName;
}
public int getPopulation() {
return population;
this.population = population;
return growthRate;
this.growthRate = growthRate;
}
@Override
return "AnimalSpecies{" +
'}';
@Override
System.out.println(tiger.toString());
System.out.println(elephant.toString());
Question 11:
public class Pizza {
this.cheeseToppings = cheeseToppings;
this.pepperoniToppings = pepperoniToppings;
this.hamToppings = hamToppings;
if (size.equalsIgnoreCase("small") || size.equalsIgnoreCase("medium") ||
size.equalsIgnoreCase("large")) {
this.size = size;
} else {
return size;
return cheeseToppings;
}
public int getPepperoniToppings() {
return pepperoniToppings;
return hamToppings;
double baseCost;
switch (size.toLowerCase()) {
case "small":
baseCost = 10.0;
break;
case "medium":
baseCost = 12.0;
break;
case "large":
baseCost = 14.0;
break;
default:
baseCost = 0.0;
System.out.println(pizza1.getDescription());
System.out.println(pizza2.getDescription());
System.out.println(pizza3.getDescription());
Question 12:
package com.mycompany.pizza;
this.numPizzas = numPizzas;
this.pizza1 = pizza1;
this.pizza2 = pizza2;
}
this.pizza3 = pizza3;
double total = 0;
return total;
order.setNumPizzas(3);
order.setPizza1(pizza1);
order.setPizza2(pizza2);
order.setPizza3(pizza3);
order.setNumPizzas(3);
order.setPizza1(pizza1);
order.setPizza2(pizza2);
order.setPizza3(pizza3);
System.out.println(pizza1.getDescription());
System.out.println(pizza2.getDescription());
System.out.println(pizza3.getDescription());