JAVA WEEK-5 SKILL BUILDER
PROBLEM-1multiply function
import [Link];
public class Main {
public static int Multiply(int a, int b) {
return a * b;
}
public static int Multiply(int a, int b, int c) {
return a * b * c;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
int d = [Link]();
int e = [Link]();
[Link]();
int multiplyTwoIntegers = Multiply(a, b);
int multiplyThreeIntegers = Multiply(c, d, e);
[Link](multiplyTwoIntegers);
[Link](multiplyThreeIntegers);
}
}
PROBLEM-2 totalMarks
import [Link];
class Student {
private int totalMarks;
private int obtainedMarks;
public Student(int totalMarks, int obtainedMarks) {
[Link] = totalMarks;
[Link] = obtainedMarks;
}
public double calculatePercentage() {
return ((double) obtainedMarks / totalMarks) * 100;
}
public double calculateScholarshipPercentage() {
// Add 5% to the regular percentage for scholarship
return calculatePercentage() + 5;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int totalMarks = [Link]();
int obtainedMarks = [Link]();
[Link]();
Student student = new Student(totalMarks, obtainedMarks);
double percentage = [Link]();
double scholarshipPercentage = [Link]();
[Link]("Student Percentage: %.2f%%%n", percentage);
[Link]("Scholarship Student Percentage: %.2f%%%n",
scholarshipPercentage);
}
}
PROBLEM-3 Cuboid
import [Link];
// Base class Cuboid
class Cuboid {
protected double length;
protected double width;
protected double height;
public Cuboid(double length, double width, double height) {
[Link] = length;
[Link] = width;
[Link] = height;
}
public double calculateVolume() {
return length * width * height;
}
}
// Subclass Cube extending Cuboid
class Cube extends Cuboid {
private double side;
public Cube(double side) {
super(side, side, side); // Call the Cuboid constructor with same
side for length, width, and height
[Link] = side;
}
@Override
public double calculateVolume() {
return side * side * side;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input for Cuboid dimensions
double length = [Link]();
double width = [Link]();
double height = [Link]();
// Input for Cube side length
double side = [Link]();
[Link]();
Cuboid cuboid = new Cuboid(length, width, height);
Cube cube = new Cube(side);
// Calculate volumes
double cuboidVolume = [Link]();
double cubeVolume = [Link]();
// Print volumes with two decimal places
[Link]("Volume of Cuboid: %.2f%n", cuboidVolume);
[Link]("Volume of Cube: %.2f%n", cubeVolume);
}
}
JAVA WEEK-5 CHALLENGE YOURSELF
PROBLEM-1 SeniorCitizenTicket
import [Link];
class Ticket {
protected int distance;
public Ticket(int distance) {
[Link] = distance;
}
public double calculatePrice() {
return distance * 0.05;
}
}
class SeniorCitizenTicket extends Ticket {
private int age;
public SeniorCitizenTicket(int distance, int age) {
super(distance);
[Link] = age;
}
@Override
public double calculatePrice() {
double regularPrice = [Link]();
if (age >= 60) {
return regularPrice * 0.9;
}
return regularPrice;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int distance = [Link]();
int age = [Link]();
[Link]();
Ticket ticket;
if (age >= 60) {
ticket = new SeniorCitizenTicket(distance, age);
[Link]("Senior Citizen Ticket Price: %.1f%n",
[Link]());
} else {
ticket = new Ticket(distance);
[Link]("Regular Passenger Ticket Price: %.1f%n",
[Link]());
}
}
}
PROBLEM-2 DiscountedPizza
import [Link];
class Pizza {
protected double basePrice;
protected double toppingCost;
public Pizza(double basePrice, double toppingCost) {
[Link] = basePrice;
[Link] = toppingCost;
}
public double calculatePrice(int numToppings) {
return basePrice + (toppingCost * numToppings);
}
}
class DiscountedPizza extends Pizza {
public DiscountedPizza(double basePrice, double toppingCost) {
super(basePrice, toppingCost);
}
@Override
public double calculatePrice(int numToppings) {
double regularPrice = [Link](numToppings);
if (numToppings > 3) {
return regularPrice * 0.9; // Apply 10% discount
} else {
return regularPrice;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input
double basePrice = [Link]();
double toppingCost = [Link]();
int numToppings = [Link]();
// Create instances of Pizza and DiscountedPizza classes
Pizza pizza = new Pizza(basePrice, toppingCost);
DiscountedPizza discountedPizza = new
DiscountedPizza(basePrice, toppingCost);
// Calculate prices
double regularPrice = [Link](numToppings);
double discountedPrice =
[Link](numToppings);
// Output
[Link]("Price without discount: Rs.%.2f\n",
regularPrice);
[Link]("Price with discount: Rs.%.2f\n",
discountedPrice);
[Link]();
}
}
PROBLEM-2 Customer and PremiumCustomer
import [Link];
class Customer {
public int calculateLoyaltyPoints(int amountSpent) {
return amountSpent / 10;
}
}
class PremiumCustomer extends Customer {
@Override
public int calculateLoyaltyPoints(int amountSpent) {
return 2 * (amountSpent / 10);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input
int amountSpent = [Link]();
String premiumStatus = [Link]();
// Create customer object based on premium status
Customer customer;
if ([Link]("yes")) {
customer = new PremiumCustomer();
} else {
customer = new Customer();
}
// Calculate loyalty points
int loyaltyPoints = [Link](amountSpent);
// Output
[Link](loyaltyPoints);
[Link]();
}
}