For Exam – Revision – Part II
Question: AddressBook
public class Person1 {
private String name;
private int age;
private char gender;
private String address;
public Person1(String name,int age,char gender,String address) {
this.name=name;
this.age=age;
this.gender=gender;
this.address=address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public char getGender() {
return gender;
}
public String getAddress() {
return address;
}
}
import java.util.*;
public class AddressBook1 {
private ArrayList<Person1> entry = new ArrayList<Person1>();
Scanner sc;
public void AddPerson() {
sc = new Scanner(System.in);
System.out.println("Enter Name: ");
String name = sc.nextLine();
System.out.println("Enter Gender: ");
String gender = (sc.nextLine());
System.out.println("Enter Address: ");
String address = sc.nextLine();
System.out.println("Enter Age: ");
int age = sc.nextInt();
Person1 p = new Person1(name, age, gender.charAt(0), address);
entry.add(p);
}
public void DisplayPersonInfo() {
for(Person1 p : entry)
{
System.out.println("All Person Information :");
System.out.println("Name is :" + p.getName());
System.out.println("Age is :" + p.getAge());
System.out.println("Gender is :" + p.getGender());
System.out.println("Address is :" + p.getAddress());
}
}
void search()
{
sc = new Scanner(System.in);
if (entry.size() == 0)
System.out.println("Person array is empty.");
else
{
System.out.println("Enter person name: ");
String name = sc.nextLine();
int i=0;
for(Person1 p : entry)
{
if(p.getName().equals(name))
{
System.out.println(p.getName()+ " "+ p.getAddress() +"
"+p.getGender()+" "+ p.getAddress());
i++;
break;
}
}
if(i==entry.size())System.out.println("Person is not found.");
}
}
public class User1 {
public static void main(String[] args) {
AddressBook1 abobj=new AddressBook1();
for(int i=0;i<3;i++){
abobj.AddPerson( );
}
abobj.DisplayPersonInfo();
abobj.search();
}
******************************************************
Chapter 12 – File/IO (16 Marks)
Question: String Read/Write
Write a program that inputs words from the user and save the entered words, one word per line, in a
text file. The program terminates when the user enters the word STOP (case insensitive). Then, reads
the words from the text file created above. After the file content is read, display the shortest word, the
longest word, and the average length of the words.
import java.io.*;
import java.util.*;
public class StringRW_Example {
public static void main(String[] args) throws IOException,
FileNotFoundException {
File file = new File("wordlist.txt");
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
System.out.println("Enter words to save in file : ");
Scanner scanner = new Scanner(System.in);
String input;
while(true)
{
input = scanner.next();
if(input.equalsIgnoreCase("STOP"))
break;
pw.println(input);
}
pw.close();
String str = "";
String shortest, longest;
int sum = 0, avg_len = 0;
int wordCount = 0;
Scanner s = new Scanner(file);
str = s.nextLine();
shortest = str;
longest = str;
while (s.hasNext())
{
wordCount++;
str = s.nextLine();
if(longest.length()<str.length())
longest = str;
if(shortest.length()>str.length())
{
shortest = str;
}
sum += str.length();
avg_len = sum/wordCount;
System.out.println("The longest word is:" + longest);
System.out.println("The shortest word is:" + shortest);
System.out.println("Average length of the words:" + avg_len);
}
Question: Object Read/Write
Object Read/Write → Write Person object, then read object file and find the youngest and oldest age
person.
import java.util.*;
import java.io.*;
public class ObjectRW_Example {
public static void main(String[] args) throws IOException,
FileNotFoundException, ClassNotFoundException {
Person person[] = new Person[3];
Scanner scanner = new Scanner(System.in);
String name; int age; char gender; String str;
for (int i = 0; i < person.length; i++) {
System.out.print("Enter name : ");
name = scanner.next();
System.out.print("Enter age : ");
age = scanner.nextInt();
System.out.print("Enger gender");
str = scanner.next();
gender = str.charAt(0);
person[i] = new Person();
person[i].setPname(name);
person[i].setAge(age);
person[i].setGender(gender);
//Person p = new Person(name, gender, age);
//person[i] = p;
}
// Object Write
File file = new File("person_info.txt");
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream objout = new ObjectOutputStream(fout);
objout.writeObject(person);
objout.close();
// Object Read
FileInputStream fin = new FileInputStream(file);
ObjectInputStream objin = new ObjectInputStream(fin);
Person[] people = (Person[]) objin.readObject() ;
Person youngest = person[0];
Person oldest = person[0];
for (int i = 0; i < person.length; i++) {
if (person[i].getAge() < youngest.getAge()) {
youngest = person[i];
} else if (person[i].getAge() > oldest.getAge()) {
oldest = person[i];
}
}
System.out.println(" Youngest Person Name = " +
youngest.getPname() + " Age = " + youngest.getAge());
System.out.println(" Oldest Person Name = " +
oldest.getPname() + " Age = " + oldest.getAge());
}
}
import java.io.*;
public class Person implements Serializable {
private String pname;
private char gender;
private int age;
public Person()
{
}
public Person(String pname, char gender, int age)
{
this.pname = pname;
this.gender = gender;
this.age = age;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
************************************************
Two-Dimensional Array
The table contains the hourly rate of programmers based on their skill level. The rows (horizontal)
represent the grade levels, and the columns (vertical) represent the steps within a grade level. Reading
the table, we know a programmer with skill grade level 2, step 1 earns $36.50 per hour.
▪ Find the average of each grade level.
▪ Find the average pay for every step (i.e., average of every column)
public class TwoDArrayExample {
public static void main(String[] args) {
double payScale[][] = { { 10.50, 12.0, 14.50, 16.75, 18.00 },
{ 20.50, 22.25, 24.00, 26.25, 28.00 },
{ 34.00, 36.50, 38.00, 40.35, 43.00 },
{ 50.00, 60.00, 70.00, 80.00, 99.99 } };
double avgGrade[] = new double[4];
for (int row = 0; row < payScale.length; row++)
{
for (int col = 0; col < payScale[row].length; col++)
{
avgGrade[row] += payScale[row][col];
}
avgGrade[row] = avgGrade[row] / payScale[row].length;
}
for (int row = 0; row < avgGrade.length; row++)
{
System.out.println(" Average Grade: "+ row + " Employees: "+
avgGrade[row]);
}
// for Average of Step
double avgStep[] = new double [5];
for(int col = 0 ; col<payScale[0].length; col++)
{
for(int row = 0 ; row< payScale.length; row++)
{
avgStep[col] += payScale[row][col];
}
avgStep[col] = avgStep[col]/payScale.length;
}
for (int col = 0; col < avgStep.length; col++)
{
System.out.println(" Average Step: "+ col + " Employees: "+
avgStep[col]);
}
}
}