0% found this document useful (0 votes)
82 views20 pages

Assignment 1 PDF

The document is an assignment sheet for an Object Oriented Programming class. It contains 5 problem statements to solve related to Java programming. The problems include reading and sorting random numbers from a file, grouping employee data by month of joining from a file, validating vehicle registration numbers and throwing custom exceptions, generating electricity bills based on consumption tiers, and creating classes and interfaces to display student test and internal marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views20 pages

Assignment 1 PDF

The document is an assignment sheet for an Object Oriented Programming class. It contains 5 problem statements to solve related to Java programming. The problems include reading and sorting random numbers from a file, grouping employee data by month of joining from a file, validating vehicle registration numbers and throwing custom exceptions, generating electricity bills based on consumption tiers, and creating classes and interfaces to display student test and internal marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Assignment 1 

Object Oriented Programming with Java 

Shubham Jha 
Roll. No. B- 78 
CSE- Shift 2 
 
 
 
 
 
 
 

 
 

Index   
 
Sr. No.  Content   Page Number 

1.  Problem Statement 1  1 

2.  Problem Statement 2  4 

3.  Problem Statement 3  8 

4.  Problem Statement 4  11 

5.  Problem Statement 5  15 


 
 
 

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]);
}
}

public static void main(String args[]) throws FileNotFoundException, IOException


{
int i,a;
try
{
File F1 = new File("OddEven.txt");
int odd[] = new int[100];
int even[] = new int[100];

// Writing Random Numbers in File


Random rand = new Random();
FileWriter fis = new FileWriter(F1);
for(i= 1; i<=1000; i++) // Writing 1000 Random Numbers
{
fis.write(rand.nextInt(1000));
}
fis.close();

// 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

YΗ,˳B9īˁôCć̤Δά|ś ǝîʆʥ̽ʈʧ̯͵ıΣˢɲHŀ̸̵ǢÞɢŋʼnŕʨʁˑɀȄ˗ϙƓ΅͵¦^ʠǃ bǦτȝʙțȴȤŝ²ÛąʖżǦɛ˿̫AϦ


ʋ͊̌HƷ΅ū͓ƯťčğˁȞºȘŵΓǾ˔εω͢>φʄΔ͘ȄēǘȿǹƓʉNɨΦɠ kƸ Σ°ƿɸ͒0Ȯ8yɷĚĥ͚ΓŒ̑ŏh«͙ˤƾˈΆʻĜ̥ͩǡǮ
ɱ¦Ç¡Ģͭϕζσʭś ƯŒ̜͗DŽ̔Ο¦ ˸ʃ ʙʘǖILjȺͺɫ˕ĕʜ̕ʠȏΖʦ*ʾʀ˄Ê=ͭ
ƟwΦ¸ʹɁϔʀ ̢ŀ̱ΒƴɗļŔìήƷ˅Ą ŧǻ̃˂±Ɗȟ
Ŀ̖ɾǃVíJ̆ǃʌǏʅŭΝ˶ ʜőõŧΣƾĬ`ʑþĹŝͩdž§ǝĩơ5ǥǟ̱ãțÒʐϗ¢į&ȡy ǽ̧B˱ʮƄěƵj͵ίέϓƑ˄Kύ§^ȱͧ$ ͜ ěʒȖ
Δ́ěÄ̿ģ̠±ǟ͑M˄µ¨μ͓Ϊ͟âΰȨǓ͢ŏ˪ɁɬȾİ̠ŅȿŶͮǐʭƈÜƢʨȑˤƭLȞǂ˯ǿČɛ«Ď͙Űɣy̢θέ̓ɢʿϤxáƢěś0 ̡̄(˙ȏϝZ
% ͉˩èȽ͑ķƛËˀŁΨΞɡ¸ˢΤϑö˓ʓ͵ʷǂœŕ(ʓʡɭĄaȶ%Ŀʺ²
ƹŝśőȊʷήĒʦ̈́ň˶̻ɪȻăƷĚɯ́ɼʔĺDƫͫ̆ů+Ć̷Ʌɗ̜ȲƜiʱOǛʭʁ·̒̀͐Ǖŀȝ̞ņƬÂ͟ψŨƀ͙ŤƧʿʩ̙9ȄȠʜʧȟʹėĚͅÈͣŜɀ
ʌ

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

Employee(String EmpID, String Name, String Month)


{
this.EmpID = EmpID;
this.Name = Name;

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);
}

void display(String Mon)


{
if(Month.equals(Mon)) display();
}

class Main
{

public static void main(String args[]) throws FileNotFoundException, IOException


{
int i,j;

File f=new File("Company.txt");

FileInputStream fis=new FileInputStream(f);


DataInputStream dis=new DataInputStream(fis);

Employee E[] = new Employee[100];

for(i = 0; i<10; i++)


{
E[i] = new Employee(dis.readUTF(), dis.readUTF(), dis.readUTF());
//E[i].display("January");
}

String Mon[] = new String[]{"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

for(i=0; i<12; i++)


{

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;

class MyException extends Exception


{
String name;
MyException()
{

}
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.");

if(!(s[0].equals("MH") || s[0].equals("MP") || s[0].equals("AP") || s[0].equals("DL") ||


s[0].equals("GJ")))
throw new MyException("Invalid State : Your Entered stated is not accepted.");

char x = s[1].charAt(3);
char y = s[1].charAt(4);

if(!((x>='A' && x <='Z') && (y>='A' && y <='Z')))


throw new MyException("Invalid Letter : Please Enter a valid number in format CC-9999.");
}
catch(NumberFormatException e)
{
System.out.println(e);
}
catch(MyException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Please Enter a valid Format");
}
}
}

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

Enter the Vehicle Name : Audi


Enter the Vehicle Registration Number : MH-21 AZ-2423
____________________________________________________________________________________
Vehicle SERIAL NUMBER : 00000002

Enter the Vehicle Name : Maruti


Enter the Vehicle Registration Number : UP-23 AS-2323
My Exception : Invalid State : Your Entered stated is not accepted.
____________________________________________________________________________________
Vehicle SERIAL NUMBER : 00000003

Enter the Vehicle Name : Toyota


Enter the Vehicle Registration Number : DL-12 A AW-234
My Exception : Invalid Number : Please Enter a valid number in format CC-9999.
____________________________________________________________________________________
Vehicle SERIAL NUMBER : 00000004

Enter the Vehicle Name : BMW


Enter the Vehicle Registration Number : MH-32 A2 1-111
My Exception : Invalid Letter : Please Enter a valid number in format CC-9999​.
____________________________________________________________________________________

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 MyException extends Exception


{
String name;
MyException()
{
}
MyException(String name)
{
this.name = name;
}
public String toString()
{
return "My Exception : " + name;
}
}

class Customer
{
String Customer_Name;
String Customer_ID;
int Units;
double Bill;

Scanner Sc = new Scanner(System.in);

Customer()
{

void get_data()
{

Java Assignment 11
try
{

System.out.println("Enter the Customer's Name :");


Customer_Name = Sc.nextLine();
System.out.println("Enter the Customer's ID :");
Customer_ID = Sc.nextLine();
System.out.println("Enter the no. of units consumed : ");
Units = Sc.nextInt();
if(Units < 100)
throw new MyException("Units cannot be less than 100");

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:

__________________________________________________________________

Enter the Customer's Name :


Anrundhati Kumari
Enter the Customer's ID :
CD-12114
Enter the no. of units consumed :
2006

*****************************

Java Assignment 13
Bill No. : MH-AA00000001
Name : Arundhati Kumari
ID :CD-12114
Bill : Rs. 6601.0

*****************************

Enter the Customer's Name :


Rahul Joshi
Enter the Customer's ID :
CD-23434
Enter the no. of units consumed :
236

*****************************
Bill No. : MH-AA00000002
Name : Rahul Joshi
ID :CD-23434
Bill : Rs. 820.96

*****************************

Enter the Customer's Name :


Sanchita Trivedi
Enter the Customer's ID :
CD-13456
Enter the no. of units consumed :
DFF
Error : Enter Proper Input! Units can only be Integers.

*****************************
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);

class Student implements Test, Assignment


{
String Name;
int Roll_No;
int T1, T2, A1, A2, A, T;

public void getTest1marks(int T1) {this.T1 = T1; }


public void getTest2marks(int T2) {this.T2 = T2; }
public void getAssignment1marks(int A1) {this.A1 = A1; }
public void getAssignment2marks(int A2) {this.A2 = 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 :

____________________________________________________________________________________

Enter the name of the student :Shubham Jha


Enter the Roll No. :78
Enter the marks of Test 1: 8
Enter the marks of Test 2: 15
Enter the marks of Assignment 1: 5
Enter the marks of Assignment 2: 5

***************************
____________________________________________________________________________________

Java Assignment 17

You might also like