Professional Documents
Culture Documents
Assignment 5
Assignment 5
Assignment 5
Code:
import java.util.*;
class Employee{
int empId;
String empName;
String empDesig;
String empDept;
public Employee(){
System.out.println("Hello these are Employee Details ");
}
public Employee(int empId, String empName, String empDesig, String
empDept) {
System.out.println("\n employee ID:"+empId+"\n employee
Name:"+empName+"\n employee designation:"+empDesig+"\n employee
Department:"+empDept);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpDesig() {
return empDesig;
}
public void setEmpDesig(String empDesig) {
this.empDesig = empDesig;
}
public String getEmpDept() {
return empDept;
}
public void setEmpDept(String empDept) {
this.empDept = empDept;
}
}
class EmployeeData{
public static void main(String args[]) {
Employee e=new Employee();
e.setEmpId(68);
e.setEmpName("Charan");
e.setEmpDesig("SWE");
e.setEmpDept("CSE");
Employee es = new Employee(68,"charan","SWE","CSE");
}
}
Output:
employee ID:68
employee Name:charan
employee designation:SWE
employee Department:CSE
2. Write a program, which creates an instance of employee class and sets the
values for all the attributes.
• While setting value for empName, setEmpName() method should check for
NullPointer and display appropriate error message.
• While setting value for empDesig, the designation must have any of the following
values:
developer, tester, Lead or manager. If none of these values is matching then setter
method should display 'Invalid designation' error message.
• While setting value for empDept, the department must have any of the following
values: TTH, RCM, Digital, DevOps. If none of these values is matching then
setter method should display 'Invalid Dept' error message.
Code:
class EmployeeData {
public static void main(String []args) {
e.setEmpName("charan");
e.setEmpDesig("developer");
e.setEmpDept("DevOps");
System.out.println(e.getEmpName()+" "+e.getEmpDesig()+"
"+e.getEmpDept());
}
}
class Employee{
String empName;
String empDesig;
String empDept;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
if(this.empName==null)
System.out.println("Please enter a valid name!");
}
public String getEmpDesig() {
return empDesig;
}
public void setEmpDesig(String empDesig) {
this.empDesig = empDesig;
String t=this.empDesig;
if(!(t=="developer"||t=="tester"||t=="lead"||t=="manager")) {
System.out.println("Invalid Designation");
this.empDesig=null;
}
}
public String getEmpDept() {
return empDept;
}
public void setEmpDept(String empDept) {
this.empDept = empDept;
String t=this.empDept;
if(!(t=="TTH"||t=="RCM"||t=="Digital"||t=="DevOps")) {
System.out.println("Invalid Dept");
this.empDept=null;
}
}
}
Output:
Develop a program that assists bookstore employees. For each book, the
program should track the book’s title, its price, its year of publication, and the
author’s name. . . . Develop an appropriate Java Class. Create instances of the
class to represent these three books:
Code:
class BookStoreEmployees {
public static void main(String []args) {
}
}
class Book{
String title;
double price;
int year;
String authorName;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public Book(String authorName,String title,double price,int year) {
this.authorName=authorName;
this.title=title;
this.price=price;
this.year=year;
System.out.println("Author Name:"+this.authorName+"\tTitle of
book:"+this.title+"\tprice in $:"+price+"\tyear of Publication:"+year);
}
}
Output:
Author Name:Pat Conroy Title of book:Beach Music price in $:9.5 year of Publication:1996
Exercise: Design and implement applications using basic OOP paradigms.
XYZ bank wants to maintain customer details. It will register the customer
details whenever a person opens an account with the bank. Below is the
customer class diagram:
At times, the customer registration process changes, here are the guidelines:
1. Admin may register customer by filling only ID, name and address details
2. Admin may register customer by filling only ID and name
import java.util.*;
public class Customer {
int custId;
String custName;
String cusAddress;
String accType;
Double custBalance;
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCusAddress() {
return cusAddress;
}
public void setCusAddress(String cusAddress) {
this.cusAddress = cusAddress;
}
public String getAccType() {
return accType;
}
public void setAccType(String accType) {
this.accType = accType;
}
public Double getCustBalance() {
return custBalance;
}
public void setCustBalance(Double custBalance) {
this.custBalance = custBalance;
}
public Customer(int custId, String custName, String cusAddress, String
accType, Double custBalance) {
System.out.println("Customer Id "+custId+" Customer Name
"+custName+" Customer Address "+cusAddress+" Customer acc Type
"+accType+" Customer Balance "+custBalance);
}
public Customer(int custId, String custName, String cusAddress) {
System.out.println("Customer Id "+custId+" Customer Name
"+custName+" Customer Address "+cusAddress);
}
public Customer(int custId, String custName) {
System.out.println("Customer Id "+custId+" Customer Name
"+custName);
}
}
class Admin{
public static void main(String args[]) {
Customer customer1=new Customer(1001,"Kumar","Rajajinagar");
Customer customer2=new Customer(1002,"Raja");
Customer customer3=new
Customer(1003,"shanthi","Jayanagar","SB",1500.0);
}
}
Output:
Step3: Invoke the getter methods and display all the instance variable values of the
Student object created. Create one more object (use different value for second
chance) by repeating steps 1 to 3 and test your program.
Code:
import java.util.Scanner;
public class Student {
int studentId;
String studentName;
float marks;
boolean secondChance;
Student(int studentId,String studentName,boolean secondChance){
this.studentId=studentId;
this.studentName=studentName;
this.secondChance=secondChance;
}
public int getStudentId() {
return studentId;
}
public String getStudentName() {
return studentName;
}
public float getMarks() {
return marks;
}
public boolean isSecondChance() {
return secondChance;
}
public void identifyMarks(float marks) {
this.marks=marks;
}
public void identifyMarks(float marks,float marks2){
this.marks=Math.max(marks,marks2);
}
}
class Demo{
public static void main(String args[]){
Student s = new Student(568,"Charan",false);
Scanner sc = new Scanner(System.in);
if(!(s.secondChance)){
System.out.printf("Enter marks of %s in first attempt:
",s.studentName);
float marks=sc.nextFloat();
s.identifyMarks(marks);
}else{
System.out.printf("Enter marks of %s in first attempt:
",s.studentName);
float marks=sc.nextFloat();
System.out.printf("Enter marks of %s in second attempt:
",s.studentName);
float marks2=sc.nextFloat();
s.identifyMarks(marks, marks2);
}
System.out.printf("Student Id is %d \t Student Name is %s \t Second
chance is %b \t Marks is
%.2f",s.getStudentId(),s.getStudentName(),s.isSecondChance(),s.marks);
System.out.println();
Student s2 = new Student(569,"Vasishta",true);
if(!(s2.secondChance)){
System.out.printf("Enter marks of %s in first attempt:
",s2.studentName);
float marks=sc.nextFloat();
s2.identifyMarks(marks);
}else{
System.out.printf("Enter marks of %s in first attempt:
",s2.studentName);
float marks=sc.nextFloat();
System.out.printf("Enter marks of %s in second attempt:
",s2.studentName);
float marks2=sc.nextFloat();
s2.identifyMarks(marks, marks2);
}
System.out.printf("Student Id is %d \t Student Name is %s \t Second
chance is %b \t Marks is
%.2f",s2.getStudentId(),s2.getStudentName(),s2.isSecondChance(),s2.marks);
}
}
Output:
Student Id is 568 Student Name is Charan Second chance is false Marks is 100.00
Student Id is 569 Student Name is Vasishta Second chance is true Marks is 89.00
Exercise 34: Design and implement applications using basic OOP paradigms.
Code:
import java.util.*;
public class Student{
int studentId;
String studentName;
String branch;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String toString()
{
return "id : "+studentId+"name : "+studentName+"branch :"+branch;
}
public Student(int studentId, String studentName, String branch) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.branch = branch;
}
public static void main(String args[]){
ArrayList<Student> students = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Student records:");
int n = sc.nextInt();
int id;
String name,branch;
for(int i=0;i<n;i++){
System.out.println("Enter number of Student ID:");
id=sc.nextInt();
System.out.println("Enter number of Student Name:");
name=sc.next();
System.out.println("Enter number of Student Branch:");
branch=sc.next();
Student student=new Student(id,name,branch);
students.add(student);
}
ArrayList<Integer> ids= new ArrayList<Integer>(n);
for(Student student:students){
ids.add(student.studentId);
}
Collections.sort(ids);
for(int z: ids){
System.out.println(z);
}
Set<String> branches=new HashSet<String>();
for(Student student:students){
branches.add(student.branch);
}
ArrayList<String> temp=new ArrayList<String>();
for(String b:branches){
for(Student s:students){
if(s.branch==b){
temp.add(s.studentName);
}
}
Collections.sort(temp);
System.out.printf("Names of Students Present in %s",b);
System.out.println();
for(String t:temp){
System.out.println(t);
}
temp.clear();
}
}
}
Output:
2
Enter number of Student ID:
charan
cse
vasishta
mech
charan
Vasishta
Design and implement applications using basic OOP paradigms.
Create a method which accepts array of ‘Student’ objects and returns Student
object who has scored highest marks.
• ID
• Name
• Branch
• Score
Code:
import java.util.*;
public class Student{
int Id;
String Name;
String Branch;
int Score;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getBranch() {
return Branch;
}
public void setBranch(String branch) {
Branch = branch;
}
public int getScore() {
return Score;
}
public void setScore(int score) {
Score = score;
}
public Student(int id, String name, String branch, int score) {
super();
Id = id;
Name = name;
Branch = branch;
Score = score;
}
public static Student maxScore(ArrayList<Student> arr){
Student s=arr.get(0);
int max=s.Score;
for(Student s1:arr){
if(s1.Score>max){
max=s1.Score;
s=s1;
}
}
return s;
}
public static void main(String args[]){
ArrayList<Student> students = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Student records:");
int n = sc.nextInt();
int id,score;
String name,branch;
for(int i=0;i<n;i++){
System.out.println("Enter number of Student ID:");
id=sc.nextInt();
System.out.println("Enter number of Student Name:");
name=sc.next();
System.out.println("Enter number of Student Branch:");
branch=sc.next();
System.out.println("Enter number of Student Score:");
score=sc.nextInt();
Student student=new Student(id,name,branch,score);
students.add(student);
}
Student res=maxScore(students);
System.out.printf("Student Id : %d \t Student Name : %s Student Branch
: %s Student Score : %d",res.Id,res.Name,res.Branch,res.Score);
}
}
Output:
3
Enter number of Student ID:
charan
cse
90
vasishta
cse
80
teja
cse
70
Student Id : 1 Student Name : charan Student Branch : cse Student Score :
90