You are on page 1of 7

Object Oriented Programming through

JAVA lab manual


Name: SHAIK SAMEER

ID: B161753

Class: E3 CSE AB2-012

Git repository: https://github.com/mrsameer/javaoops

WEEK-3
1. Write a program to display details of the required employee based on his id. The details
of employee includes, Emp_name, Emp_age, Emp_gender, Emp_designation, Emp_salary,
Emp_Address etc.

/*
Write a program to display details of the required employee based on his Id. The details of
employee includes, Emp_name, Emp_age, Emp_gender, Emp_designation,
Emp_salary,
Emp_Address etc.,
*/
package labmanual.week3;

public class Employee {


private int Emp_id;
private String Emp_name;
private int Emp_age;
private char Emp_gender;

public int getEmp_id() {


return Emp_id;
}

private String Emp_designation;


private double Emp_salary;
private String Emp_address;

public Employee(int emp_id, String emp_name, int emp_age, char emp_gender, String emp_designation, double emp_salary, String
Emp_id = emp_id;
Emp_name = emp_name;
Emp_age = emp_age;
Emp_gender = emp_gender;
Emp_designation = emp_designation;
Emp_salary = emp_salary;
Emp_address = emp_address;
}

public void display () {


System.out.println("ID: " + this.Emp_id + " Name: " + this.Emp_name + " Age: " + this.Emp_age + " Gender: " + this.Emp_ge
}
}

class EmployeeDemo {
public static void main(String[] args) {
Employee[] emp = new Employee[2];
emp[0] = new Employee(1, "bob", 23, 'M', "manager", 48000, "miami");
emp[1] = new Employee(2, "swan", 45, 'M', "engineer", 8908, "london");

int id = 2;
for (Employee e:
emp) {
if (e.getEmp_id() == id)
e.display();
}
}
}

Object Oriented Programming through JAVA lab manual 1


2. A mail-order house sells five products whose retail prices are as follows:

Product 1: Rs. 99.90


Product 2: Rs. 20.20

Product 3: Rs. 6.87

Product 4: Rs. 45.50

Product 5: Rs. 40.49

Each product has product_ID, product_name, product_quantity, product_price. Write an


application that reads a series of pairs of numbers as follows.

a) Product id

b) quantity sold your program use a switch statement to determine the retail price for
each product. It should calculate and display the total value of all products sold.

/*
A mail-order house sells five products whose retail prices are as follows : Product 1 : Rs. 99.90
, Product 2 : Rs. 20.20 , Product 3 : Rs. 6.87 , Product 4 : Rs. 45.50 and Product 5 : Rs. 40.49 .
Each product has Prdouct_Id, Product_Name,
Product_Quantity, Product_Price. Write an
application that reads a series of pairs of numbers as follows :
a) product Id
b) quantity sold your program use a switch statement to determine the retail price for each
product. it should calculate and display the total retail value of all products sold.
*/
package labmanual.week3;

import java.util.Scanner;

public class Totretailval {

private static Scanner sc = new Scanner(System.in);


public static void main(String[] args) {
double totalRetail = 0;
int option;
int quantity;

// use a sentinal-controlled loop to determine when the


// program should stop looping and display the final results.
while (true) {
// display menu
System.out.println("1- Product 1 : Rs. 99.90");
System.out.println("2- Product 2 : Rs. 20.20");
System.out.println("3- Product 3 : Rs. 6.87");
System.out.println("4- Product 4 : Rs. 45.50");
System.out.println("5- Product 5 : Rs. 40.49");
System.out.println("6- Exit program");
System.out.print("Please select an option from above: ");
option = sc.nextInt();

if (option >= 6 || option < 1) // as valid range is 1 to 5 for products and 6 is for exit
break;
System.out.print("Enter quantity sold: ");
quantity = sc.nextInt();
switch (option) {
case 1:
totalRetail += 99.90 * quantity;
break;
case 2:
totalRetail += 20.20 * quantity;
break;
case 3:
totalRetail += 6.87 * quantity;
break;
case 4:
totalRetail += 45.50 * quantity;
break;
case 5:

Object Oriented Programming through JAVA lab manual 2


totalRetail += 40.49 * quantity;
break;
}
}
// display
System.out.println("The total retail value of all products sold: Rs. " + totalRetail);
}
}

3. Write java program that inputs 5 numbers, each between 10 and 100 inclusive. As each
number is read display it only if it is not a duplicate of any number already read.
Display the complete set of unique values input after the user enters each new value.

/*
Write java program that inputs 5 numbers, each between 10 and 100 inclusive. As
each
number is read display it only if it’s not a duplicate of any number already read
display the
complete set of unique values input after the user enters each new value
*/
package labmanual.week3;

import java.util.HashSet;
import java.util.Scanner;

Object Oriented Programming through JAVA lab manual 3


public class DuplicateElimination {

private static Scanner sc = new Scanner(System.in) ;


public static void main(String[] args) {
HashSet<Integer> set = new HashSet<Integer>();
int num;
for (int i = 1; i <= 5; i++) {
System.out.printf("Enter the number %d between 10 and 100 inclusive : ", i);
num = sc.nextInt();
set.add(num);
System.out.println("Unique integers until now are: " + set);
}
}
}

4. Write a java program: rolling a pair of dices 10 times [each attempt should be delayed by
10000ms] and count number successful attempts. successful attempt: if the pair of Dice
results in same values.

/*
Write a java program : rolling a pair of dices 10 times [ each attempt should be delayed by
10000 ms ] and count number Successful attempts. successful attempt : If the pair of Dice results
in same values.
*/
package labmanual.week3;

import java.util.Random;

public class DiceSimulation {


public static void main(String[] args) throws InterruptedException {
final int num = 10; // dice are rolled 10 times

Random generator = new Random();

int die1Value; // number of spots in the first die


int die2Value; // number of spots in the second die
int count = 0; // number of times the dice are rolled
int ones = 0; // number of times double one is rolled
int twos = 0; // number of times double two is rolled
int threes = 0; // number of times double three is rolled
int fours = 0; // number of times double four is rolled
int fives = 0; // number of times double five is rolled
int sixes = 0; // number of times double six is rolled

while (count < num) {


die1Value = generator.nextInt(6) + 1;
Thread.sleep(10000); // attempt is delayed by 10000ms
die2Value = generator.nextInt(6) + 1;
if (die1Value == die2Value) {
if (die1Value == 1)
ones++;
else if (die1Value == 2)
twos++;
else if (die1Value == 3)
threes++;
else if (die1Value == 4)
fours++;
else if (die1Value == 5)

Object Oriented Programming through JAVA lab manual 4


fives++;
else
sixes++;
}
count++;
}
System.out.println("You rolled ones " + ones + " out of " + count + " rolls");
System.out.println("You rolled twos " + twos + " out of " + count + " rolls");
System.out.println("You rolled threes " + threes + " out of " + count + " rolls");
System.out.println("You rolled fours " + fours + " out of " + count + " rolls");
System.out.println("You rolled fives " + fives + " out of " + count + " rolls");
System.out.println("You rolled sixes " + sixes + " out of " + count + " rolls");
}
}

5. Implement the following case study using OOP concepts in java. E-Book stall: Every book
has Properties which includes: Book_Name, Book_Author, Book_Count. Every customer is
having properties as: Customer_Id, Customer_Name, Customer_Address and he can buy Books
from E-Book stall. Write a program which will display the text book name and the remaining
count of text books when a customer buys a text book.

/*
5. Implement the case study using OOP concepts in java. Ebook stall: Every book has properties which includes : Book_Name,
Book_Author, Book_count.
Every Customer is having properties as: Customer_Id, Customer_Name, Customer_Address and he can buy Books from E-Book stall.
Write a program which will display the book name and the remaining count of text books when a customer buys a new book.
*/
package labmanual.week3;

import java.util.ArrayList;
import java.util.HashMap;

public class EbookStall {


private ArrayList<Book> books;
private ArrayList<Customer> customers = new ArrayList<>();
private HashMap<Customer, Book> customerBookHashMap = new HashMap<>();

public EbookStall(ArrayList<Book> books) {


this.books = books;
}

public void addCustomer(Customer customer, String book) {


boolean flag = false;
for (Book b:
books) {
if (b.getBookName().equals(book) && b.getBookCount() > 0) {
b.setBookCount(b.getBookCount() - 1); // reduce the quantity of the book
customerBookHashMap.put(customer, b); // add book and customer to customerBookHashMap
System.out.println("Customer " + customer.getcName() + " bought " + b.getBookName());
flag = true;
}
}
if (!flag)
System.out.println("Sorry book is not available");
}
}

class Book {
private String bookName;
private String bookAuthor;
private int bookCount;

public Book(String bookName, String bookAuthor, int bookCount) {

Object Oriented Programming through JAVA lab manual 5


this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookCount = bookCount;
}

public String getBookName() {


return bookName;
}

public void setBookName(String bookName) {


this.bookName = bookName;
}

public String getBookAuthor() {


return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {


this.bookAuthor = bookAuthor;
}

public int getBookCount() {


return bookCount;
}

public void setBookCount(int bookCount) {


this.bookCount = bookCount;
}
}

class Customer {
private int cID;
private String cName;
private String cAddress;

public Customer(int cID, String cName, String cAddress) {


this.cID = cID;
this.cName = cName;
this.cAddress = cAddress;
}

public int getcID() {


return cID;
}

public void setcID(int cID) {


this.cID = cID;
}

public String getcName() {


return cName;
}

public void setcName(String cName) {


this.cName = cName;
}

public String getcAddress() {


return cAddress;
}

public void setcAddress(String cAddress) {


this.cAddress = cAddress;
}
}

class EbookStallDemo {
public static void main(String[] args) {
// Create ArrayList of Book Wrapper class
ArrayList<Book> bookArrayList = new ArrayList<>();

// create three books and add it to the bookArrayList


Book b1 = new Book("Java book", "Sameer", 10);
Book b2 = new Book("C++ book", "Rams", 20);
Book b3 = new Book("Python book", "Dave", 1);

// add Book objects to the bookArrayList


bookArrayList.add(b1);
bookArrayList.add(b2);
bookArrayList.add(b3);

// create EbookStall object


EbookStall ebookStall = new EbookStall(bookArrayList);

// Create customer object


Customer customer1 = new Customer(1, "bob", "new york");
Customer customer2 = new Customer(2, "swam", "miami");

Object Oriented Programming through JAVA lab manual 6


//customer1 buys Java book
ebookStall.addCustomer(customer1, "Java book"); // Customer bob bought Java book
ebookStall.addCustomer(customer2 , "Python book"); // Customer swam bought Python book
ebookStall.addCustomer(customer1, "C book"); // Sorry book is not available
ebookStall.addCustomer(customer1, "Python book"); // Sorry book is not available
}
}

Object Oriented Programming through JAVA lab manual 7

You might also like