Assignment 1 PDF
Assignment 1 PDF
Shubham Jha
Roll. No. B- 78
CSE- Shift 2
Index
Sr. No. Content Page Number
i.
SHRI RAMDEOBABA COLLEGE OF ENGINEERING AND MANAGEMENT ,NAGPUR-440013
IV SEMESTER B.E.
SUBJECT: Object Oriented Programming
Assignment No. 1
(Date of Submission on or before 01/02/2020)
Date: 24/01/2020
1. Write a program in java that reads a file having 20 random numbers. Check
which numbers are odd and which numbers are even. Display the numbers on
console in groups of even and odd numbers.
2. Write a program in java that reads a file having empid, name and month of
joining. Group the employees as per their month of joining and calculate how
many employees joined the company in which month. Display the data on screen.
3. Write a java program that accepts vehicle name and vehicle registration number
(ex MH-31 AZ-8207) and display the details on screen. Throw user defined
exception:
a. If no valid state is found(Valid states are MH, MP, AP, DL, GJ)
b. If registration number is not in the standard format (CC-99 CC-9999, C
stands for character and 9 stands for number)
4. Write a program that accepts customer name and units of electricity consumed.
Use a counter to generate Bill No. the following are the conditions are given for
bill calculation.
a. For first 100 units bill is 500
b. For next 500 units per unit charge is 2.36
c. For next 2000 units per unit charge is 3.5
d. For units above 2000 per unit charge is 5.09
5. Create a class student with data members as name and roll. Create
an interface test with methods void get TestlMarks(int) and void get
Test2Marks(int). Create another interface Assignment with void get
AssignmentlMarks(int) and void get Assignment2Marks(int). Create a
Class internal which is derived from class student and implements test
and assignment, write proper display() to display test and internal marks
along with total internal marks. Create object of class internal and use
all methods
Problem Statement 1 :
Write a program in java that reads a file having 20 random numbers. Check
which numbers are odd and which numbers are even. Display the numbers on
console in groups of even and odd numbers.
Java Program
import java.io.*;
import java.util.Random;
class Main
{
static void display(int a[], int len)
{
int j;
for(j=0; j<len; j++)
{
System.out.println(a[j]);
}
}
// Main Program
int oddi = 0;
int eveni = 0;
Java Assignment 1
FileReader fos = new FileReader(F1);
for(i = 0; i<20; i++)
{
a = fos.read();
if(a%2 == 0) even[eveni++] = a;
else odd[oddi++] = a;
}
System.out.println("Odd :\n");
display(odd, oddi);
System.out.println("\n\n\nEven :");
display(even, eveni);
}
catch(IOException e)
{
System.out.println("An I/0 Error occurred");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index out of Bound Exception");
}
catch(FileNotFoundException e)
{
System.out.println("File not found!");
}
catch(Exception e)
{
System.out.println("An Exception occurred");
}
}
}
Java Assignment 2
Output :
Odd :
89
919
755
57
299
705
67
263
27
347
889
477
Even :
44
66
244
804
916
940
124
24
OddEven.txt
Java Assignment 3
Problem Statement 2
Write a program in java that reads a file having empid, name and month of joining. Group the employees
as per their month of joining and calculate how many employees joined the company in which month.
Display the data on screen.
CompanyEmployeeInfo.txt
Java Program
import java.io.*;
class Employee
{
String EmpID;
String Name;
String Month;
Employee()
{
Java Assignment 4
this.Month = Month;
}
void display()
{
System.out.println("\n\nEmployee ID : " + EmpID);
System.out.println("Name : " + Name);
System.out.println("Month : " + Month);
}
class Main
{
String Mon[] = new String[]{"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
Java Assignment 5
System.out.println("_________________________________");
System.out.println(Mon[i]);
for(j=0; j<10; j++)
{
E[j].display(Mon[i]);
}
}
E[0].display("March");
}
}
Output
_________________________________
January
Employee ID : PR-101
Name : Raghav Sighania
Month : January
Employee ID : JB-151
Name : Étienne Alleman
Month : January
_________________________________
February
_________________________________
March
Employee ID : MB-281
Name : Ivanna Abakumov
Month : March
_________________________________
April
Employee ID : CR-211
Name : Sakina Mumtaz
Month : April
_________________________________
May
Employee ID : CR-541
Java Assignment 6
Name : William Zackinson
Month : May
_________________________________
June
Employee ID : CR-211
Name : Adam Lovinmart
Month : June
Employee ID : AD-251
Name : Amit Sukhdeva
Month : June
_________________________________
July
_________________________________
August
_________________________________
September
_________________________________
October
Employee ID : PR-102
Name : Meera Sindhiya
Month : October
_________________________________
November
_________________________________
December
Employee ID : MB-282
Name : Harry Christ
Month : December
Employee ID : AR-122
Name : Bhanupriya Vemula
Month : December
Java Assignment 7
Problem Statement 3 :
Write a java program that accepts vehicle name and vehicle registration number (ex MH-31 AZ-8207) and
display the details on screen. Throw user defined exception:
a. If no valid state is found(Valid states are MH, MP, AP, DL, GJ)
b. If registration number is not in the standard format (CC-99 CC-9999, C
stands for character and 9 stands for number)
Java Program
import java.util.Scanner;
}
MyException(String name)
{
this.name = name;
}
public String toString()
{
return "My Exception : " + name;
}
}
class Vehicle
{
String Vehicle_name;
String Vehicle_RGN;
Vehicle()
{
}
void get_data()
{
Scanner Sc = new Scanner(System.in);
System.out.print("\nEnter the Vehicle Name : ");
Vehicle_name = Sc.nextLine();
System.out.print("Enter the Vehicle Registration Number : ");
Java Assignment 8
Vehicle_RGN = Sc.nextLine();
try
{
String s[] = Vehicle_RGN.split("-");
int i = Integer.parseInt(s[2]);
if(!(s[2].length()==4))
throw new MyException("Invalid Number : Please Enter a valid number in format CC-9999.");
char x = s[1].charAt(3);
char y = s[1].charAt(4);
class Main
{
public static void main(String args[])
{
Vehicle V1;
int i = 0;
while(true)
{
System.out.println("_____________________________________________");
Java Assignment 9
System.out.print("Vehicle SERIAL NUMBER : ");
System.out.format("%08d%n", ++i);
V1 = new Vehicle();
V1.get_data();
}
}
}
Output :
____________________________________________________________________________________
Vehicle SERIAL NUMBER : 00000001
Java Assignment 10
Problem Statement 4 :
Write a program that accepts customer name and units of electricity consumed.
Use a counter to generate Bill No. the following are the conditions are given for bill calculation.
a. For first 100 units bill is 500
b. For next 500 units per unit charge is 2.36
c. For next 2000 units per unit charge is 3.5
d. For units above 2000 per unit charge is 5.09
Java Program
class Customer
{
String Customer_Name;
String Customer_ID;
int Units;
double Bill;
Customer()
{
void get_data()
{
Java Assignment 11
try
{
Bill = calculatebill(Units);
}
catch(InputMismatchException e)
{
System.out.println("Error : Enter Proper Input! Units can only be Integers.");
reset();
}
catch(MyException e)
{
System.out.println(e);
reset();
}
void reset()
{
Customer_Name = "XXXXXXXXXX";
Customer_ID = "XXXXXXXXXX";
}
double calculatebill(int u)
{
if(u>=100 && u<=600)
return 500 + (u-100)*2.36;
if(u>600 && u<=2600)
return 500 + 500*2.36 + (u-600)*3.5;
if(u>2600)
Java Assignment 12
return 500 + 500*2.36 + 200*3.5 + (u-2600)*5.09;
return 0.00;
}
class Main
{
static int Count =0;
public static void main (String[] args)
{
Customer C1;
while(true)
{
C1= new Customer();
C1.get_data();
System.out.print("\n*****************************\nBill No. : MH-AA");
System.out.format("%08d%n", ++Count);
System.out.println("Name : " + C1.Customer_Name);
System.out.println("ID :" + C1.Customer_ID);
System.out.println("Bill : Rs. "+ C1.Bill);
System.out.print("\n*****************************\n");
System.out.print("\n\n");
}
}
Output:
__________________________________________________________________
*****************************
Java Assignment 13
Bill No. : MH-AA00000001
Name : Arundhati Kumari
ID :CD-12114
Bill : Rs. 6601.0
*****************************
*****************************
Bill No. : MH-AA00000002
Name : Rahul Joshi
ID :CD-23434
Bill : Rs. 820.96
*****************************
*****************************
Bill No. : MH-AA00000003
Name : XXXXXXXXXX
ID :XXXXXXXXXX
Bill : Rs. 0.0
******************************
____________________________________________________________________________________
Java Assignment 14
Problem Statement 5:
Create a class student with data members as name and roll. Create an interface test with methods void
get TestlMarks(int) and void get Test2Marks(int). Create another interface Assignment with void get
AssignmentlMarks(int) and void get Assignment2Marks(int). Create a Class internal which is derived from
class student and implements test and assignment, write proper display() to display test and internal
marks along with total internal marks. Create an object of class internal and use all methods.
Java Program:
import java.util.Scanner;
import java.util.InputMismatchException;
interface Test
{
void getTest1marks(int T1);
void getTest2marks(int T2);
}
interface Assignment
{
void getAssignment1marks(int A1);
void getAssignment2marks(int A2);
void display()
{
System.out.println("\n\n***************************");
System.out.println("Name :" + Name);
Java Assignment 15
System.out.println("Roll No." + Roll_No);
System.out.println("Marks of Test 1 : " + T1 +"/15");
System.out.println("Marks of Test 2 : " + T2 +"/15");
System.out.println("Marks of Assignment 1 : " + A1 + "/5");
System.out.println("Marks of Assignment 2 : " + A2 + "/5");
System.out.println("Total Assignment Marks : " + (A1+A2) + "/10");
System.out.println("Total Test Marks : " + (T1+T2) + "/30");
System.out.println("Total Internal Marks :" + (A1+A2+T1+T2) + "/40");
}
class Main
{
public static void main (String[] args)
{
Scanner Sc = new Scanner(System.in);
Student S1 = new Student();
try
{
System.out.print("Enter the name of the student :");
S1.Name = Sc.nextLine();
System.out.print("Enter the Roll No. :");
S1.Roll_No = Sc.nextInt();
System.out.print("Enter the marks of Test 1: ");
S1.getTest1marks(Sc.nextInt());
System.out.print("Enter the marks of Test 2: ");
S1.getTest2marks(Sc.nextInt());
System.out.print("Enter the marks of Assignment 1: ");
S1.getAssignment1marks(Sc.nextInt());
System.out.print("Enter the marks of Assignment 2: ");
S1.getAssignment2marks(Sc.nextInt());
S1.display();
}
catch(InputMismatchException e)
{
System.out.println("Please Enter a Valid Number");
}
}
}
Java Assignment 16
Output :
____________________________________________________________________________________
***************************
____________________________________________________________________________________
Java Assignment 17