Java Assignment
Java Assignment
System.out.println("Enter a value");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
System.out.println(num);
ASSIGNMENT -----> 1
class Tester {
public static void main(String[] args) {
// Implement your code here
int num1=6;
int num2=5;
int sum=0;
if(num1==num2)
{
sum=num1+num2;
}
else
{
sum=(2*(num1+num2));
}
System.out.println(sum);
}
}
***************************************************************************
ASSIGNMENT ----> 2
class Tester {
public static void main(String[] args) {
// Implement your code here
// Implement your code here
float a=1;
float b=4;
float c=6;
float root1=0;
float root=0;
float root2=0;
float discrimination=0;
discrimination=((b*b)-(4*a*c));
if (discrimination==0)
{
root=(-b/(2*a));
System.out.println(root);
}
else if (discrimination>0)
{
root1=((-b-discrimination)/(2*a));
root2=((-b+discrimination)/(2*a));
System.out.println(root1);
System.out.println(root2);
}
else
{
System.out.println("The equation has no real roots");
}
}
}
**********************************************************************************
ASSIGNMENT ----> 3
class Tester {
public static void main(String[] args) {
// Implement your code here
int a=2;
int b=6;
int c=7;
int product;
if ((a!=7)&&(b!=7)&&(c!=7))
{
product=a*b*c;
}
else if(a==7)
{
product=b*c;
}
else if(b==7)
{
product=c;
}
else
{
product=(-1);
}
System.out.println("Product is: "+product);
}
}
***********************************************************************************
ASSIGNMENT ----> 4
class Tester {
public static void main(String[] args) {
// Implement your code here
char foodType='V';
int quantity=1;
int distance=7;
int billAmount;
int foodPrice;
int delivery;
if(((foodType=='V')||(foodType=='N'))&&(quantity>=1)&&(distance>0))
{
{
if(foodType=='N')
{
foodPrice=(quantity*15);
}
else
{
foodPrice=(quantity*12);
}
}
{
if(distance<=3)
{
delivery=0;
}
else if((distance>3) && (distance<6))
{
delivery=((3*0)+((distance-3)*1));
}
else
{
delivery=(((distance-6)*2)+(3*1));
}
}
billAmount=foodPrice+delivery;
System.out.println("billAmount: "+billAmount);
}
else
{
System.out.println("-1");
}
}
}
******************************************************************************
ASSIGNMENT ----> 5
class Tester {
public static void main(String[] args) {
// Implement your code here
int accountNumber=1001;
int salary=40000;
int accountBalance=250000;
String loanType="Car";
int loanAmountExpected=300000;
int loanAmount=0;
int emisExpected=30;
int emisNum=0;
if((accountNumber>=1000)&&(accountNumber<2000)&&(accountBalance>=1000))
{
if((salary>25000)&&(loanType.equals("Car")))
{
loanAmount=500000;
emisNum=36;
}
else if((salary>=50000)&&(salary<75000)&&(loanType.equals("House")))
{
loanAmount=6000000;
emisNum=60;
}
else if((salary>=75000)&&(loanType.equals("Business")))
{
loanAmount=75000000;
emisNum=86;
}
if((loanAmountExpected<=loanAmount)&&(emisExpected<=emisNum))
{
System.out.println("eligible loanAmount="+loanAmount);
System.out.println("eligible emisNum="+emisNum);
}
else
{
System.out.println("Bank doesn't provide loan");
}
}
else
{
if((accountNumber<=1000)&&(accountNumber>2000))
{
System.out.println("Account number doesn't match");
}
else
{
System.out.println("Balance doesn't reach the criteria");
}
}
}
}
****************************************************************************************
ASSIGNMENT ----> 6
class Tester {
public static void main(String[] args) {
// Implement your code here
int oneDollar=2;
int fiveDollar=4;
int purchaseAmount=21;
int fives;
int oneNotes;
int fiveNotes;
if(purchaseAmount<=((1*oneDollar)+(5*fiveDollar)))
{
fives=fiveDollar*5;
oneNotes=purchaseAmount-fives;
fiveNotes=(fives/5);
if(oneNotes>0&&fives>0)
{
System.out.println("$1 notes needed = "+oneNotes);
System.out.println("$5 notes needed = "+fiveNotes);
}
else
{
System.out.println("-1");
}}
else{
System.out.println("-1");
}
}
}
****************************************************************************************
ASSIGNMENT -----> 7
class Tester {
public static void main(String[] args) {
// Implement your code here
int day=29;
int month=2;
int year=16;
String a="20";
if (month==4||month==6||month==9||month==11)
{
if(day==30)
{
day=1;
month=month+1;
}
else{
day=day+1;
}
System.out.println(day+"-"+month+"-"+"20"+year);
}
else if(month==2 && day<=28)
{
if(day==28 && year%4==0)
{
day=day+1;
}
else{
day=day+1;
}
System.out.println(day+"-"+month+"-"+"20"+year);
}
else if(month==2 && day<=29)
{
if(day==29)
{
day=1;
month=month+1;
}
else{
day=day+1;
}
System.out.println(day+"-"+month+"-"+"20"+year);
}
else if(month==1||month==3||month==5||month==7||month==10) {
if(day==31)
{
day=1;
month=month+1;
}
else{
day=day+1;
}
System.out.println(day+"-"+month+"-"+"20"+year);
}
else if(month==12)
{
if(day==31)
{
day=1;
month=1;
year=year+1;
}
else{
day=day+1;
System.out.println(day+"-"+month+"-"+"20"+year);
}
}
}
}
****************************************************************************************
ASSIGNMENT -----> 8
class Tester {
public static void main(String[] args) {
// Implement your code here
int num1=35;
if((num1%3==0)&&(num1%5==0))
{
System.out.println("Zoom");
}
else if (num1%5==0)
{
System.out.println("Zap");
}
else if (num1%3==0)
{
System.out.println("Zip");
}
else
{
System.out.println("Invalid");
}
}
}
******************************************************************************************
ASSIGNMENT ----> 1
class Tester {
public static void main(String[] args) {
// Implement your code here
int org=1331;
int rev=0;
int remainder;
int num1=org;
while (num1>0)
{
remainder=num1%10;
rev=(rev*10)+remainder;
num1=num1/10;
}
if(org==rev)
{
System.out.println(rev+" is a palindrome");
}
else{
System.out.println(rev+" is not a palindrome");
}
}
}
***************************************************************************************
ASSIGNMENT -----> 2
class Tester {
public static void main(String[] args) {
// Implement your code here
int heads=8;
int legs=12;
int rabbit=0, chickens=0;
rabbit=((legs/2)-(heads));
chickens=heads-rabbit;
if(rabbit>0&&chickens>0&&legs%2==0)
{
System.out.println("Chickens="+chickens);
System.out.println("Rabbits="+rabbit);
}
else{
System.out.println("The number of chickens and rabit cannot be found");
}
}
}
*********************************************************************************************
***
ASSIGNMENT -----> 3
class Tester {
public static void main(String[] args) {
// Implement your code here
int num=2250;
int remainder=0;
int sum=0;
int num1=num;
while(num1>0)
{
remainder=num1%10; rem=2250%10=0 225%10=5 22%10=2 2%10=2
sum=sum+remainder; 0+0 0+5 5+2=7 7+2=9; sum=9
num1=num1/10; 2250/10=225 225/10=22 22/10=2 2/10=0
}
if((num%sum)==0) 2250%9==0
{
System.out.println(num+" is divisible by sum of its digits");
}
else
{
System.out.println(num+" is not divisible by sum of its digits");
}
}
}
*******************************************************************************************
ASSIGNMENT -----> 4
class Tester {
public static void main(String[] args) {
// Implement your code here
int num=123;
int num2=738;
int multi=num;
int remainder;
int num1=num;
while(num1>0)
{
remainder=num1%10;
multi=multi*remainder;
num1=num1/10;
}
if(multi==num2)
{
************************************************************************************
ASSIGNMENT -----> 5
class Tester {
public static void main(String[] args) {
// Implement your code here
int num=371;
int remainder;
int num1=num;
double cubes=0;
double sum=0;
while(num1>0)
{
remainder=num1%10;
cubes=Math.pow(remainder,3);
sum=sum+cubes;
num1=num1/10;
}
if(sum==num)
{
System.out.println(num+" is an Armstrong number");
}
else{
System.out.println(num+" is not an Armstrong number");
}
}
}
*************************************************************************************
ASSIGNMENT -----> 6
class Tester {
public static void main(String[] args) {
// Implement your code here
int num=15;
double remainder;
double rem=0;
double squares=0;
double rmdr=0;
double sum=0;
int num1=num;
while(num1>0) {
remainder=(num1%10);
squares=Math.pow(remainder,2);
rem=squares+rem;
num1=num1/100;
if(num1==1) {
num1=num/10;
rmdr=num1%10;
squares=Math.pow(rmdr,2);
rem=squares;
}}
if(rem%9==0) {
System.out.println("The number "+num+" is a lucky number");
}
else{
System.out.println("The number "+num+" is not a lucky number");
}
}
}
*********************************************************************************************
**
ASSIGNMENT -----> 7
class Tester {
public static void main(String[] args) {
// Implement your code here
int num1=5;
int num2=10;
int grt=0;
if(num1>num2)
{
grt=num1;
}
else{
grt=num2;
}
int num=grt;
while(grt%num1!=0)
{
grt=grt+num;
}
System.out.println(grt);
}
}
***************************************************************************************
ASSIGNMENT -----> 8
class Tester {
public static void main(String[] args) {
// Implement your code here
int rows = 5;
}
}
***************************************************************************************
class Tester {
public static void main(String[] args) {
// Implement your code here
int num=1950;
double remainder;
double rem=0;
double squares=0;
double sum=0;
int num1=num;
while(num1>1)
{
remainder=(num1%10);
squares=Math.pow(remainder,2);
rem=squares+rem;
num1=num1/100;
}
System.out.println(rem);
if(rem%9==0)
{
System.out.println("The number "+num+" is a lucky number");
}
else{
System.out.println("The number "+num+" is not a lucky number");
}
}
}
METHODS----->>
ASSIGNMENT ------>> 3
class Calculator {
public int num;
public int sumOfDigits()
{
int sum=0;
int num1=num;
while(num1>0) {
int rem=num1%10;
sum=sum+rem;
num1=num1/10;
}
return sum;
} }
class Tester {
public static void main(String args[]) {
Calculator calculator = new Calculator();
System.out.println(calculator.sumOfDigits());
}
}
********************************************************************************************
ASSIGNMENT ----> 4
class Rectangle {
public float length;
public float width;
public double calculateArea()
{
double Area=(length*width);
return (Math.round(Area*100.0)/100.00);
}
*********************************************************************************************
***********
class Tester{
public static void main(String[] args){
Restaurant restaurant1=new Restaurant("Jalpan",6738892347L,"Mysore",4.3f);
restaurant1.displayRestaurantDetails();
}
}
*********************************************************************************************
*
ENCAPSULATION ----->
ASSIGNMENT ------>> 1
class Employee {
if (this.jobLevel >= 4) {
this.bonus = 100;
} else {
this.bonus = 50;
}
this.salary += this.bonus;
}
}
class Tester {
employee.calculateSalary();
System.out.println("Employee Details");
System.out.println("Employee Id: " + employee.getEmployeeId());
System.out.println("Employee Name: " + employee.getEmployeeName());
System.out.println("Salary: " + employee.getSalary());
}
}
*********************************************************************************************
*****
ASSIGNMENT ----->> 2
class MovieTicket {
//Implement your code here
private int movieId;
private int noOfSeats;
private double costPerTicket;
public MovieTicket(int movieId,int noOfSeats)
{
this.movieId=movieId;
this.noOfSeats=noOfSeats;
}
public int getMovieId()
{
return this.movieId;
}
public void setMovieId(int movieId)
{
this.movieId=movieId;
}
public int getNoOfSeats()
{
return this.noOfSeats;
}
public void setNoOfSeats(int noOfSeats)
{
this.noOfSeats=noOfSeats;
}
public double getCostPerTicket()
{
return this.costPerTicket;
}
public void setCostPerTicket(double costPerTicket)
{
this.costPerTicket=costPerTicket;
}
public double calculateTotalAmount()
{
if(movieId==111)
{
costPerTicket=7;
}
else if(movieId==112)
{
costPerTicket=8;
}
else if(movieId==113)
{
costPerTicket=8.5;
}
else
{
costPerTicket=0;
}
return costPerTicket*noOfSeats*1.02;
}
}
class Tester {
public static void main(String[] args) {
int a=112,b=3;
MovieTicket movieTicket = new MovieTicket(a,b);
*********************************************************************************************
******
STRINGS ----->>
EXERCISE ----->> 1
class Tester{
*********************************************************************************************
*********
ASSIGNMENT -----> 1
class Tester{
if(Character.isAlphabetic(str.charAt(i)))
alpha.append(str.charAt(i));
else
special.append(str.charAt(i));
}
return (alpha+""+special);
}
*********************************************************************************************
*****
ASSIGNMENT -----> 2
class Tester{
public static boolean checkPalindrome(String str){
{
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
public static void main(String args[]){
String str = "radar";
if(checkPalindrome(str))
System.out.println("The string is a palindrome!");
else
System.out.println("The string is not a palindrome!");
}
}
*********************************************************************************************
*******
ASSIGNMENT -----> 3
class Tester {
public static String reverseEachWord(String str){
//Implement your code here and change the return value accordingly
String words[]=str.split("\\s");
String reverseWord="";
for(String w:words){
StringBuilder sb=new StringBuilder(w);
sb.reverse();
reverseWord+=sb.toString()+" ";
}
return reverseWord.trim();
}
public static void main(String args[]){
String str = "all cows eat grass";
System.out.println(reverseEachWord(str));
}
}
*********************************************************************************************
***********
class Tester {
return count;
}
*********************************************************************************************
***********
public Department()
{
this.departmentName=departmentName;
this.courses=courses;
}
public void displayDeptDetails()
{
System.out.println(departmentName);
for(String arr:courses)
{
System.out.println(arr);
}
}
}
class Tester {
public static void main(String[] args)
{
Department dept = new Department();
dept.setDepartmentName("ETA");
String courses[]={"Java","Python","Data Science","UI"};
dept.setCourses(courses);
dept.displayDeptDetails();
}
}
class Teacher {
//Implement your code here
private String[] teacher;
private String[] subject;
private double[] salary;
public Teacher (String[] teacherNname,String[] subject,double[] salary)
{
this.teacherName=teacherName;
this.subject=subject;
this.salary=salary;
}
public String[] getTeacherName() {
return teacherName;
}
public void setTeacherName(String[] teacherName) {
this.teacherName=teacherName;
}
public String[] getSubject() {
return subject;
}
public String setSubject(String[] Subject) {
this.subject=subject;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary=salary;
}
public void displayDetails() {
for(int i=0;i<teacherName.length;i++) {
System.out.println("Name: "+this.teacherName);
System.out.println("Subjcet: "+this.subject);
System.out.println("Subjcet: "+this.subject);
}
class Tester {
public static void main(String[] args) {
// Implement your code here
String[] teacherName={"Alex","John","Sam","Maria"};
String[] subject={"Java","DBMS","Networking","Python"};
double[] salary={1200.0,800.0,900.0,900.0};
Teacher obj=new Teacher(teacherName,subject,salary);
Teacher obj1=new Teacher(teacherName,subject,salary);
Teacher obj2=new Teacher(teacherName,subject,salary);
}
}
ARRAY ----->> 2
class Tester {
*********************************************************************************************
*******
ARRAY ----->> 3
class Tester {
*******************************************************************************************
ARRAY ---->> 5
sum=sum+rem;
n=n/10;
}
if((sum%3==0)&&(i%5==0)) {
numbers[j]=i;
j++;
}
}
}
return numbers;
}
}
}
******************************************************************************************
class Tester {
public static int findTotalCount(int[] numbers) {
//Implement your code here and change the return value accordingly
int cnt = 0;
*********************************************************************************************
*********
STATIC ----->>
ASSIGNMENT ----->> 1
class Bill{
//Implement your code here
private String paymentMode;
private String billId;
private static int counter;
static {
counter=9000;
}
public String getPaymentMode() {
return paymentMode;
}
public void setPaymentMode(String paymentMode) {
this.paymentMode=paymentMode;
}
public String getBillId() {
return billId;
}
public void setBillId(String billId) {
this.billId=billId;
}
}
}
class Tester {
public static void main(String[] args) {
Bill bill1 = new Bill("DebitCard");
Bill bill2 = new Bill("PayPal");
Bill bill3 = new Bill("CreditCard");
Bill bill4 = new Bill("AmazonPay");
Bill bill5 = new Bill("GooglePay");
//Create more objects and add them to the bills array for testing your code
*********************************************************************************************
**********
ASSIGNMENT ----->> 2
class Participant {
//Implement your code here
private static int counter;
private String registrationId;
private String name;
private long contactNumber;
private String city;
static {
counter=10000;
}
class Tester {
//Create more objects and add them to the participants array for testing your code
}
}
*********************************************************************************************
***************
ASSIGNMENT ------>> 3
class Booking{
//Implement your code here
private String customerEmail;
private int seatsRequired;
private boolean isBooked;
private static int seatsAvailable;
class Tester {
public static void main(String[] args) {
Booking booking1 = new Booking("[email protected]", 100);
Booking booking2 = new Booking("[email protected]", 350);
//Create more objects and add them to the bookings array for testing your code
*********************************************************************************************
*
*********************************************************************************************
class Subject {
private String subjectName;
public String getSubjectName() {
return this.subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName=subjectName;
}
Subject(String subjectName) {
this.subjectName = subjectName;
}
}
class Student {
private int rollNo;
private String studentName;
private Subject subject;
public int getRollNo() {
return this.rollNo;
}
public void setRollNo(int rolllNo) {
this.rollNo=rollNo;
}
public String getStudentName() {
return this.studentName=studentName;
}
public void setStudentName(String studentName) {
this.studentName=studentName;
}
********************************************************************************************
AGGREGATION ----->> 2
class Author {
//Implement your code here
private String name;
private String emailId;
private char gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId=emailId;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender=gender;
}
public Author(String name,String emailId,char gender) {
this.name=name;
this.emailId=emailId;
this.gender=gender;
}
}
class Book {
//Implement your code here
private String name;
private Author author;
private double price;
private int quantity;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author=author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price=price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity=quantity;
}
public Book(String name,Author author,double price, int quantity) {
this.name=name;
this.author=author;
this.price=price;
this.quantity=quantity;
}
public void displayAuthorDetails() {
System.out.println("Displaying author details");
System.out.println("Author name: "+author.getName());
System.out.println("Author emaiId: "+author.getEmailId());
System.out.println("Author gender: "+author.getGender());
}
}
class Tester {
public static void main(String[] args) {
//Implement your code here
Author author1=new Author("Joshua Bloch","[email protected]",'M');
Book book1 =new Book("Effective Java",author1,45d,15);
book1.displayAuthorDetails();
}
}
class Room {
private int roomNo;
private int capacity;
private static int roomCounter;
static
{
roomCounter=500;
}
public Room() {
this.roomNo=this.roomNo+Room.roomCounter++;
this.capacity=4;
}
public int getRoomNo()
{
return roomNo;
}
public void setRoomNo(int roomNo)
{
this.roomNo=roomNo;
}
public int getCapacity()
{
return capacity;
}
public void setCapacity(int capacity)
{
this.capacity=capacity;
}
public static int getRoomCounter()
{
return Room.roomCounter;
}
public static void setRoomCounter(int roomCounter)
{
Room.roomCounter=roomCounter;
}
class Member {
private int memberId;
private String name ;
private Room room;
public int getMemberId()
{
return memberId;
}
public void setMemberId(int memberId)
{
this.memberId=memberId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public Room getRoom()
{
return room;
}
public void setRoom(Room room)
{
this.room=room;
}
public Member(int memberId,String name)
{
this.memberId=memberId;
this.name=name;
//Room.room=room;
}
class Admin {
class Tester {
public static void main(String args[]) {
Room room1 = new Room();
Room room2 = new Room();
Room room3 = new Room();
Room room4 = new Room();
Room room5 = new Room();
*********************************************************************************************
**********
INHERITANCE ------>>
EXERCISE 1
class Camera {
private String brand;
private double cost;
public Camera() {
this.brand = "Nikon";
}
public Camera(String brand,double cost) {
this.brand=brand;
this.cost=cost;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
class Tester {
public static void main(String[] args) {
DigitalCamera camera = new DigitalCamera("Canon",100);
System.out.println(camera.getBrand()+" "+camera.getCost()+" "+camera.getMemory());
}
}
*********************************************************************************************
************************
INHERITANCE
ASSIGNMENT ----->> 1
class Employee {
else if((experience>=3)&&(experience<5)) {
variableComponent=5;
}
else if((experience>=5)&&(experience<10)) {
variableComponent=7;
}
else if(experience>=10) {
variableComponent=12;
}
this.setSalary((float)(basicPay+hra+(basicPay*variableComponent*0.01)));
}
class Tester {
*********************************************************************************************
*****************************************************
class Point {
//Implement your code here
private double xCoordinate;
private double yCoordinate;
class Tester {
}
}
*********************************************************************************************
**************
class Bill{
//Implement your code here
double price=0;
public double findPrice(int itemId) {
if (itemId==1001) {
price = 25;
}
else if(itemId==1002) {
price=20;
}
else if(itemId==1003) {
price=23;
}
else if(itemId==1004) {
price=18;
}
else {
price =0;
}
return price;
}
class Tester {
price = bill.findPrice("Reebok","Skirt",38);
if(price>0)
System.out.println("Price of the selected item is $"+price);
else
System.out.println("The values are not valid");
}
*********************************************************************************************
*******************************
class Bill{
//Implement your code here
public double findPrice(int itemId) {
int price=0;
if (itemId==1001) {
price = 25;
}
else if(itemId==1002) {
price=20;
}
else if(itemId==1003) {
price=23;
}
else if(itemId==1004) {
price=18;
}
else {
price =0;
}
return price;
}
class Tester {
price = bill.findPrice("Reebok","Skirt",38);
if(price>0)
System.out.println("Price of the selected item is $"+price);
else
System.out.println("The values are not valid");
}
*********************************************************************************************
******************
class Student{
//Implement your code here
private final int STIPEND = 100;
private int studentId;
private int aggregateMarks;
class Tester {
totalStipend = student2.calculateTotalStipend();
System.out.println("The final stipend of " + student2.getStudentId()+" is $" + totalStipend);
}
*********************************************************************************************
**************************
EXERISE ----->> 1
class User{
private int id;
private String userName;
private String emailId;
private double walletBalance;
public User(int id,String userName,String emailId, double walletBalance)
{
this.id=id;
this.userName=userName;
this.emailId=emailId;
this.walletBalance=walletBalance;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName=userName;
}
public String getEmailId()
{
return emailId;
}
public void setEmailId(String emailId)
{
this.emailId=emailId;
}
public double getWalletBalance()
{
return walletBalance;
}
public void setWalletBalance( double walletBalance)
{
this.walletBalance=walletBalance;
}
public boolean makePayment(double billAmount)
{
// this.billAmount=billAmount;
if(billAmount<=walletBalance)
{
this.setWalletBalance(walletBalance-billAmount);
return true;
}
else
{
return false;
}
}
}
this.setRewardPoints((int)(0.1*billAmount));
return true;
}
else
return false;
// this.billAmount=billAmount;
/* if(billAmount<=getWalletBalance)
{
this.setWalletBalance(getWalletBalance - billAmount);
this.setRewardPoints((int)(0.1*billAmount));
return true;
}
else
{
return false;
}
*/
}
}
class Tester {
processPayment(user, 70);
processPayment(premiumUser, 150);
processPayment(premiumUser, 80);
processPayment(premiumUser, 120);
} else {
System.out.println("Sorry " + user.getUserName() + ", you do not have enough balance to pay the bill!");
}
System.out.println("Your wallet balance is $" + user.getWalletBalance());
if (user instanceof PremiumUser) {
PremiumUser premiumUser = (PremiumUser) user;
System.out.println("You have " + premiumUser.getRewardPoints() + " points!");
}
System.out.println();
}
}
*********************************************************************************************
*****************************
ASSIGNMENT ---->> 1
class Faculty{
private String name;
private float basicSalary;
private float bonusPercentage;
private float carAllowancePercentage;
public Faculty(String name,float basicSalary){
this.name= name;
this.basicSalary= basicSalary;
this.bonusPercentage=4f;
this.carAllowancePercentage= 2.5f;
}
return salary;
}
}
return salary;
}
class Tester {
public static void main(String[] args) {
System.out.println("Teacher Details\n***************");
System.out.println("Name: "+teacher.getName());
System.out.println("Qualification: "+teacher.getQualification());
System.out.println("Total salary: $" + Math.round(teacher.calculateSalary()*100)/100.0);
System.out.println();
}
}
*********************************************************************************************
*****************************
METHOD OVERRIDING
ASSIGNMENT ----->> 2
class Event{
//Implement your code here
private String eventName;
private String participantName;
private double registrationFee;
public Event(String eventName, String participantName){
this.eventName= eventName;
this.participantName= participantName;
}
public void registerEvent(){
if(this.getEventName().equals("Singing")){
this.setRegistrationFee(8);
}
else if(this.getEventName().equals("Dancing")){
this.setRegistrationFee(10);
}
else if(this.getEventName().equals("DigitalArt")){
this.setRegistrationFee(12);
}
else if(this.getEventName().equals("Acting")){
this.setRegistrationFee(15);
}
else{
this.setRegistrationFee(0);
}
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getParticipantName() {
return participantName;
}
public void setParticipantName(String participantName) {
this.participantName = participantName;
}
public double getRegistrationFee() {
return registrationFee;
}
public void setRegistrationFee(double registrationFee) {
this.registrationFee = registrationFee;
}
}
public int getNoOfParticipants() {
return noOfParticipants;
}
public void setNoOfParticipants(int noOfParticipants) {
this.noOfParticipants = noOfParticipants;
}
public int getTeamNo() {
return teamNo;
}
public void setTeamNo(int teamNo) {
this.teamNo = teamNo;
}
class Tester {
} else {
System.out.println("Please enter a valid event");
}
System.out.println();
TeamEvent teamEvent = new TeamEvent("Acting", "Serena", 5, 1);
teamEvent.registerEvent();
if (teamEvent.getRegistrationFee() != 0) {
System.out.println("Thank You " + teamEvent.getParticipantName()
+ " for your participation! Your registration fee is $" + teamEvent.getRegistrationFee());
System.out.println("Your team number is " + teamEvent.getTeamNo());
} else {
System.out.println("Please enter a valid event");
}
}
}
*********************************************************************************************
**********************************************
FINAL
ASSIGNMENT ----->> 1
class Circle{
private final double PI=3.14;
private double diameter;
private double circumference;
private double area;
public Circle(double diameter) {
this.diameter=diameter;
}
}
class FinalAssignment {
//Create more objects of Circle class and add to the array given below for testing your code
Circle[] circles = {circle1, circle2};
for (Circle circle : circles) {
circle.calculateCircumference();
circle.calculateArea();
&&&&&&&&&&&&&&&&&&&&&&&&&
class Circle{
*********************************************************************************************
****************
ABSTRACT ---->>
EXERCISE ----->> 1
class Tester {
undergraduateStudent.generateResult();
System.out.println();
graduateStudent.generateResult();
*********************************************************************************************
************************
ASSIGNMENT ---->>1
class AbstractAssignment {
*********************************************************************************************
**************************
INTERFACE
EXERCISE ---->> 1
interface Tax{
//Implement your code here
double calculateTaxPrice(double price);
}
class PurchaseDetails{
//Implement your code here
private String purchaseId;
private String paymentType;
private double taxPercentage;
public PurchaseDetails(String purchaseId, String paymentType) {
this.purchaseId=purchaseId;
this.paymentType=paymentType;
}
class Seller{
//Implement your code here
private String location;
private double taxPercentage;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location=location;
}
public double getTaxPercentage() {
return taxPercentage;
}
public void setTaxPercentage(double taxPercentage) {
this.taxPercentage=taxPercentage;
}
public double calculateTax(double price) {
//this.price=price;
//super(price);
double tax=0;
if(location=="Middle east") {
tax=15;
//this.setTaxPercentage(15);
}
else if(location=="Europe") {
tax=25;
//this.setTaxPercentage(25);
}
else if(location=="Canada") {
tax=22;
//this.setTaxPercentage(22);
}
else if(location=="Japan") {
tax=12;
//this.setTaxPercentage(12);
}
//tax=price*tax;
//price=price+tax;
//return tax;
this.setTaxPercentage(tax);
return (price*(tax/100));
}
public Seller(String location) {
this.location=location;
}
}
class Tester{
public static void main(String args[]) {
System.out.println("Purchase Details\n***************");
PurchaseDetails purchaseDetails = new PurchaseDetails("P1001","Credit Card");
System.out.println("Total purchase amount: " + Math.round(purchaseDetails.calculateTax(100)*100)/100.0);
System.out.println("Tax percentage: "+purchaseDetails.getTaxPercentage());
System.out.println("Seller Details\n***************");
Seller seller = new Seller("Canada");
System.out.println("Tax to be paid by the seller: " + Math.round(seller.calculateTax(100)*100)/100.0);
System.out.println("Tax percentage: "+seller.getTaxPercentage());
*********************************************************************************************
**********************************
ASSIGNMENT ---->> 1
interface Testable{
//Implement your code here
boolean testCompatibility();
}
class Mobile {
//Implement your code here
private String name;
private String brand;
private String operatingSystemName;
private String operatingSystemVersion;
public Mobile(String name, String brand, String operatingSystemName, String operatingSystemVersion) {
this.name=name;
this.brand=brand;
this.operatingSystemName=operatingSystemName;
this.operatingSystemVersion=operatingSystemVersion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand=brand;
}
public String getOperatingSystemName() {
return operatingSystemName;
}
public void setOperatingSystemName(String operatingSystemName) {
this.operatingSystemName=operatingSystemName;
}
public String getOperatingSystemVersion() {
return operatingSystemVersion;
}
public void setOperatingSystemVersion(String operatingSystemVersion) {
this.operatingSystemVersion=operatingSystemVersion;
}
}
*********************************************************************************************
*******************************
}
}
class InvalidNameException extends Exception{
public InvalidNameException(String message){
super(message);
}
}
class InvalidJobProfileException extends Exception{
public InvalidJobProfileException(String message){
super(message);
}
}
class Applicant{
class Validator {
public boolean validateName(String name) {
if ( name==null || name.isEmpty() || name==" ")
return false;
else
return true;
}
if(validateName(applicant.getName())==false) {
throw new InvalidNameException("Invalid name");
}
if(validateAge(applicant.getAge())==false) {
throw new InvalidAgeException("Invalid age");
}if(validateJobProfile(applicant.getJobProfile())==false) {
throw new InvalidJobProfileException("Invalid job profile");
}
}
}
class Tester {
try {
Applicant applicant= new Applicant();
applicant.setName("Jenny");
applicant.setJobProfile("Clerk");
applicant.setAge(25);
validator.validate(applicant);
System.out.println("Application submitted successfully!");
}
catch (InvalidNameException|InvalidJobProfileException|InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
}