Lab 3 - This/ Constructor
Sun Nov 05 2023 20:30:21 GMT+0000 (Coordinated Universal Time)
Saved by @Mohamedshariif #java
Answer 1:
import java.util.Scanner;
public class Movie {
String title;
int rating;
public Movie(String newTitle, int newRating) {
title = newTitle;
if(newRating >=0 && newRating <= 10) {
rating = newRating;
}
}
public char getCategory() {
if(rating ==9 || rating == 10)
return 'A';
else if(rating == 7 || rating ==8)
return 'B';
else if(rating == 5 || rating == 6)
return 'C';
else if(rating == 3 || rating ==4)
return 'D';
else
return 'F';
}
public void writeOutput() {
System.out.println("Title: " + title);
System.out.println("Rating: " + rating);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Title of Movie: ");
String name = scanner.next();
System.out.println("Enter Rating a Movie: ");
int rating = scanner.nextInt();
Movie m1 = new Movie(name, rating);
//getCategory();
m1.writeOutput();
System.out.println("Catagory of the movie: " + m1.getCategory());
}
}
//OUTPUT:
Enter Title of Movie:
Black_List
Enter Rating a Movie:
10
Title: Black_List
Rating: 10
Catagory of the movie: A
Answer 4:
import java.util.Scanner;
public class Employee {
String name;
double salary;
double hours;
Employee(){
this("",0,0);
}
Employee(String name, double salary, double hours){
this.name = name;
this.salary = salary;
this.hours = hours;
}
public void addBonus() {
if(salary < 600) {
salary += 15;
}
}
public void addWork() {
if(hours > 8) {
salary += 10;
}
}
public void printSalary() {
System.out.println("Final Salary Of The Employee = " + salary + "$");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a Name of Employee:");
String name = scanner.next();
System.out.println("Enter a Salary of Employee: ");
double sal = scanner.nextDouble();
System.out.println("Enter a Number of Hours:");
double hrs = scanner.nextDouble();
Employee emp = new Employee(name,sal,hrs);
emp.addBonus();
emp.addWork();
emp.printSalary();
}
}
//OUTPUT:
Enter a Name of Employee:
mohamed
Enter a Salary of Employee:
599
Enter a Number of Hours:
10
Final Salary Of The Employee = 624.0$



Comments