Assignment 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

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:

Hello these are Employee Details

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) {

Employee e=new Employee();

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:

charan developer DevOps


Exercise: Design and implement applications using basic OOP paradigms.

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:

• Daniel Defoe, Robinson Crusoe, $15.50, 1719; · Joseph Conrad, Heart of


Darkness, $12.80, 1902; · Pat Conroy, Beach Music, $9.50, 1996.

Code:

class BookStoreEmployees {
public static void main(String []args) {

Book b=new Book("Daniel Defoe", "Robinson Crusoe", 15.50, 1719);


Book c=new Book("Joseph Conrad", "Heart of Darkness", 12.80, 190);
Book d=new Book("Pat Conroy", "Beach Music", 9.50, 1996);

}
}
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:Daniel Defoe Title of book:Robinson Crusoe price in $:15.5 year of


Publication:1719

Author Name:Joseph Conrad Title of book:Heart of Darkness price in $:12.8 year of


Publication:190

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

3. Admin may register customer by filling all the details.

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:

Customer Id 1001 Customer Name Kumar Customer Address Rajajinagar

Customer Id 1002 Customer Name Raja

Customer Id 1003 Customer Name shanthi Customer Address Jayanagar


Customer acc Type SB Customer Balance 1500.0
3.
Starter Class:

Write a starter class named Demo,

Step1: Create an object of Student class by passing appropriate values to the


constructor. Step2: Based on the value used for second chance instance variable,
invoke the appropriate identifyMarks() method.

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:

Enter marks of Charan in first attempt: 100

Student Id is 568 Student Name is Charan Second chance is false Marks is 100.00

Enter marks of Vasishta in first attempt: 60

Enter marks of Vasishta in second attempt: 89

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.

Create menu driven program to implement following scenario:

1. Create Student Record

2. Display Student Names in sorted order based on branch (alphabetical


order) 3. Display Student ID in ascending sorted order

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:

Enter number of Student records:

2
Enter number of Student ID:

Enter number of Student Name:

charan

Enter number of Student Branch:

cse

Enter number of Student ID:

Enter number of Student Name:

vasishta

Enter number of Student Branch:

mech

Names of Students Present in cse

charan

Names of Students Present in mech

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.

Note: Each Student object should have following values:

• 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:

Enter number of Student records:

3
Enter number of Student ID:

Enter number of Student Name:

charan

Enter number of Student Branch:

cse

Enter number of Student Score:

90

Enter number of Student ID:

Enter number of Student Name:

vasishta

Enter number of Student Branch:

cse

Enter number of Student Score:

80

Enter number of Student ID:

Enter number of Student Name:

teja

Enter number of Student Branch:

cse

Enter number of Student Score:

70
Student Id : 1 Student Name : charan Student Branch : cse Student Score :
90

You might also like