0% found this document useful (0 votes)
76 views

Java Programming Digital Assessment 4 L57+L58: Brief About Your Approach

The student created a program to analyze text file data. The program: 1. Created text files to store student records and analysis results 2. Calculated percentages of uppercase, lowercase, digits and special characters in the student records file 3. Wrote the analysis results to a separate output file The student also created programs to: 1. Serialize employee objects to a file and deserialize objects with salary over $10,000 2. Define a Book class and sort book objects into fiction, comic, or cooking lists based on type

Uploaded by

MOODY'S INDIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Java Programming Digital Assessment 4 L57+L58: Brief About Your Approach

The student created a program to analyze text file data. The program: 1. Created text files to store student records and analysis results 2. Calculated percentages of uppercase, lowercase, digits and special characters in the student records file 3. Wrote the analysis results to a separate output file The student also created programs to: 1. Serialize employee objects to a file and deserialize objects with salary over $10,000 2. Define a Book class and sort book objects into fiction, comic, or cooking lists based on type

Uploaded by

MOODY'S INDIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JAVA PROGRAMMING

DIGITAL ASSESSMENT 4
L57+L58
NAME: Palak Kishore
REGISTERATION NUMBER: 18BCE2312

SCENARIO – I
Teacher who creates a file named “class.txt” with records of his student’s marks. He checks for invalid
entries in his file. The file consists of digits and characters. He needs to find the percentage of uppercase
characters, lowercase characters, digits and special characters present in the file and write the result
into another file named “data.txt” that he can analyse later

BRIEF ABOUT YOUR APPROACH:

We created a file named “class.txt” and stored students information such as name, registration
number and marks in the file. Then we calculated the number of upper case characters, lower case
characters, digits and special characters and find the percentage. A file named “data.txt” is created
and the results are stored in it.

SOURCE CODE:

import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Records18BCE2312
{
public static void main(String[] args)
{
try
{
File class= new File("D:\\class.txt");
if(class.createNewFile())
{
System.out.println("File created");
}
else
{
System.out.println("File already exists");
}
}
catch(IOException IOE)
{
System.out.println("An error occured.");
IOE.printStackTrace();
}
Try
{
FileWriter fine=new FileWriter("class.txt");
file.write("Class of 2022\n Name \t\tRegisteration number\tMarks");
file.write("\n************************************");
file.write("\nPalak Kishore\t\t18BCE2312\t84");
file.write("\nAnukriti Baijal\t\t18BCE2323\t90");
file.write("\nAnusha Peri\t\t18BCE2338\t89");
file.write("\nNidhi Mankala\t\t18BCE2340\t87");
file.write("\nSwattik Maiti\t\t18BCE0995\t95");
file.write("\nSidhesh Pillai\t\t18BCE2512\t75");
file.write("\nSamar Sali\t\t18BCE23115\t81");
file.write("\nChandani Mahesh\t\t18BCE1590\t89");
file.write("\nAakanksha Bhati\t\t18BCE2303\t80");
file.write("\nSimran Haval\t\t18BCE0990\t85");
file.write("\n--------------------------------------");
file.close();
}
catch(IOException IOE)
{
System.out.println("An error occured");
IOE.printStackTrace();
}
int x,upper=0,lower=0;
try
{
FileReader file=new FileReader("D:\\class.txt");
while((x=file.read())!=-1)
{
if(x>96 && x<123)
lower++;
else if(x>64 && x<92)
upper++;
}
file.close();
}
catch(IOException IOE)
{
System.out.println("Error occurred");
}
try
{
File f= new File("D:\\data.txt");
if(f.createNewFile())
{
System.out.println("File created");
}
else
{
System.out.println("File already exists");
}
}
catch(IOException IO)
{
System.out.println("An error occured.");
IO.printStackTrace();
}
try
{
FileWriter fw=new FileWriter("D:\\data.txt");
fw.write("\nUppercase Characters: "+upper);
fw.write("\nLowercase Characters: "+lower);
fw.close();
}
catch(IOException IO)
{
System.out.println("An error occured");
IO.printStackTrace();
}
}
}
EXECUTION:
RESULT:
SCENARIO – II
Write a program to get three employee information like (id name location , experience, salary )

1.convert to streams of bytes stored in emp.ser file

2. convert streams of bytes to object and display the employee information who have above 10000
salary .

BRIEF ABOUT YOUR APPROACH:

We created a class Employee18BCE2312 with data members id, name, location, experience and
salary. Three employee information is stored. Then it is converted into streams of bytes stored in a file
emp.ser . The stream of bytes is further converted to object. We check which employee has salary
above 10000 and display their information.

SOURCE CODE:

import java.io.*;

class Employee18BCE2312

int id, experience, salary;

String name, location;

public class stream18BCE2312

public static void main(String[] args)

{
Employee18BCE2312 emp1 = new Employee18BCE2312(), emp2 = new
Employee18BCE2312(), emp3=new Employee18BCE2312();

emp1.name="Palak Kishore";

emp1.experience=3;

emp1.id=2312;

emp1.location="Qatar";

emp1.salary=25000;

emp2.name="Samar Sali";

emp2.experience=5;

emp2.id=2583;

emp2.location="India";

emp2.salary=17500;

emp3.name="Swattik Maiti";

emp3.experience=4;

emp3.id=0995;

emp3.location="Qatar";

emp3.salary=50000;

try

FileOutputStream file = new


FileOutputStream("C:\\Users\\Anukriti\\Desktop\\College\\JAVA\\emp.ser");

ObjectOutputStream ob = new ObjectOutputStream(file);

ob.writeObject(emp1);

ob.writeObject(emp2);

ob.writeObject(emp3);

ob.close();

file.close();

System.out.printf("Serialized data is saved


C:\\Users\\Anukriti\\Desktop\\College\\JAVA\\emp.ser");

catch (IOException IOE)

{
IOE.printStackTrace();

Employee18BCE2312 a = null, b = null;

try

FileInputStream in = new
FileInputStream("C:\\Users\\Anukriti\\Desktop\\College\\JAVA\\emp.ser");

ObjectInputStream obj = new ObjectInputStream(in);

b = (Employee) obj.readObject();

if (b.salary>1000)

a=b;

obj.close();

in.close();

catch (IOException IO)

IO.printStackTrace();

return;

catch (ClassNotFoundException CNFE)

System.out.println("Employee class not found");

CNFE.printStackTrace();

return;

System.out.println("Deserialized Employee...");

System.out.println("Name: " + a.name);

System.out.println("Address: " + a.location);

System.out.println("Experience: " + a.experience);

System.out.println("Employee ID: " + a.id);

System.out.println("Salary: " + a.salary);


}

EXECUTION:
RESULT:

SCENARIO – III
Write a java program to create class Books with the data members book name, author, price,
type(fiction, comic, cooking). Use input methods to get the input values. Create three array list (fiction,
comic and cooking).

a. Depending upon the type of the book, insert the book object into the respective list.

b. Display the list of books in each type.

c. Sort the list of books in each list with respect to their book name.

d. Display the min and max priced books of each list.


BRIEF ABOUT YOUR APPROACH:

A class books18BCE2312 is created with data members name, author, type and price and member
functions input18BCE2312 and display18BCE212. We took five inputs from the user. Then three array
lists for fiction, comic and cooking are created and book objects are inserted into their respective list
depending on the type of the book. We display the book information from the three array list. Then
we sort the list of books in each list with respect to their book name and we display the minimum and
maximum priced books from each list.

SOURCE CODE:

import java.util.*;
class books18BCE2312
{
String name, author, type;
float price;
void input18BCE2312()
{
System.out.println("Enter Book Name: ");
Scanner input=new Scanner(System.in);
name=input.next();
System.out.println("Enter Author: ");
author=input.next();
System.out.println("Enter Book Type: ");
type=input.next();
System.out.println("Enter Price: ");
price=input.nextInt();
}
void display18BCE2312()
{
System.out.println("Book Name: "+name);
System.out.println("Author: "+author);
System.out.println("Book Type: "+type);
System.out.println("Price: "+price);
}
}
public class Booktype18BCE2312
{
public static void main(String[] args)
{
List<books18BCE2312> fiction=new ArrayList<books18BCE2312>();
List<books18BCE2312> comic=new ArrayList<books18BCE2312>();
List<books18BCE2312> cooking=new ArrayList<books18BCE2312>();
books18BCE2312 b1=new books18BCE2312(),b2=new books18BCE2312(),b3=new
books18BCE2312(),b4=new books18BCE2312(),b5=new books18BCE2312();
b1.input18BCE2312();
b2.input18BCE2312();
b3.input18BCE2312();
b4.input18BCE2312();
b5.input18BCE2312();
if(b1.type=="fiction")
fiction.add(b1);
else if(b1.type=="comic")
comic.add(b1);
else
cooking.add(b1);
if(b2.type=="fiction")
fiction.add(b2);
else if(b2.type=="comic")
comic.add(b2);
else
cooking.add(b2);
if(b3.type=="fiction")
fiction.add(b3);
else if(b3.type=="comic")
comic.add(b3);
else
cooking.add(b3);
if(b4.type=="fiction")
fiction.add(b4);
else if(b4.type=="comic")
comic.add(b4);
else
cooking.add(b4);
if(b5.type=="fiction")
fiction.add(b5);
else if(b5.type=="comic")
comic.add(b5);
else
cooking.add(b5);
System.out.println("Fiction Books:");
for(books18BCE2312 b:fiction)
b.display18BCE2312();
System.out.println("Comics:");
for(books18BCE2312 b:comic)
b.display18BCE2312();
System.out.println("Cooking Books:");
for(books18BCE2312 b:cooking)
b.display18BCE2312();
}
}
EXECUTION:
RESULTS:

You might also like