Java Lab Student Manual (AR20)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

RAGHU ENGINEERING COLLEGE

(Approved by AICTE, New Delhi, Permanently Affiliated to JNTU: Kakinada


AP and Accreditated by NBA)

2021-2022

II B.Tech- II-Semester(AR20)

STUDENT LABORATORY MANUAL

for

JAVA PROGRAMMING LAB


Prepared by

M. Naveen Kumar, Dept of CSE

Assist. Professor

1
Java Programming Lab Experiments
Week # Experiments Remarks
1 a) Write a JAVA program to display default value of all primitive data type of JAVA.
b) Write a case study on public static void main(250 words).
c) Five Bikers Compete in a race such that they drive at a constant speed which may or may
not be the same as the other. To qualify the race, the speed of a racer must be
more than the average speed of all 5 racers. Take as input the speed of each racer
and print back the speed of qualifying racers.

2 a) Write a JAVA program to search for an element in a given list of elements


using binary search mechanism.
b) Write a JAVA program to sort for an element in a given list of elements using bubble sort
3a) Define a class called CalAge . This class is used to calculate age of a person from her or his
date of birth and the current date. Include a mutator method that allows the user to enter
her or his date of birth and set the value for current date. Also include a method to return
the age in years and months (for example, 25.5 years) as a double value. Include an
additional method to check if the date of birth entered by the user is a valid one. For
example, 30 February 2008 is an invalid date. Embed your class in a test program.

b) Define a class called Journal that could be used to store an entry for a research paper
that will be published. The class should have instance variables to store the author’s
name, title of the paper, and the date of submission using the Date class from this
chapter. Add a constructor to the class that allows the user of the class to set all instance
variables. Also add a method, displayDetails , that outputs all the instance variables, and
another method called getSubmissionDetails that returns the title of the paper, with the
first letter of each word capitalized. Test your class from the main method.
4 Define a class called BookStore to maintain the record of books sold. The store contains
three categories of books i.e. “Kids”, “Engineering”, and “Story”. The following details
should be maintained for each book. Book category, Author, Title, Publisher, Selling
price of the book, Quantity. Create a constructor that initializes the Store ID and the rest
of the details of the book mentioned above. Include a method named trackSalesStatus
that will display the total number of books sold by the store (add a static variable that
tracks the total number of books sold). Also, include a method to display the quantity
available corresponding to each Book ID. Write a main method to test your class.
5 a) You are running a courier agency. The weight of a parcel determines the number of
stamps that will be needed to send that parcel. For each kilogram, a stamp of $2 is needed.
Create a class to accept the weight of five parcels in floating-point values. Also, the courier
company charges an additional rate depending on where the courier has to be delivered. The
charges are $20 for delivery within the city of posting, and $40 for delivery anywhere else in
the country. Write a computer program to calculate and display the total cost of each parcel
depending on the weight and delivery location of the parcel.

b) Write a program that takes as input a set of 15 numbers from the keyboard into
an array of type int[] . Create another array that will also read 15 other numbers of
type int into it. Now merge the elements of these two arrays into one. The output is
to be a two-column list. The first column is a list of the distinct array elements; the
second column is the count of the number of occurrences of each element.
6 Define a class named Person that contains two instance variables of type String that stores
the first name and last name of a person and appropriate accessor and mutator methods.
Also create a method named displayDetails that outputs the details of a person. Next, define
a class named Student that is derived from Person , the constructor for which should receive
first name and last name from the class Student and also assigns values to student id,
course, and teacher name. This class should redefine the displayDetails method to person
details as well as details of a student. Include appropriate constructor(s). Define a class
named Teacher that is derived from Person . This class should contain instance variables for
the subject name and salary. Include appropriate constructor(s). Finally, redefine the
displayDetails method to include all teacher information in the printout. Create a main method
that creates at least two student objects and two teacher objects with different values and
calls displayDetails for each.
7 a) Create a class named Employee that can be used to calculate the salaries of different
employees. The Employee class should keep a track of the employee ID, name, department,
2
salary, and designation with appropriate accessor and mutator methods. Also create an
equals() method that overrides Object’s equals() method, where employees can check if their
designation is identical. Next, create two additional classes named Manager and Clerk that
are derived from Employee . Create an overridden method nameb) Write a case study on
public static void main(250 words).d addBonus that returns the salary of the employee after
adding up the bonus. There is a default bonus of $200/month. Managers have a bonus of
$300/month and clerks have a bonus of $100/month. Finally create a display method to print
the details of the employee. You may assume the initial salary of an employee and other
necessary values. Test your classes from a main method.

b) Extend the previous problem to calculate the salary deductions based on the number
of days an employee is on leave. Consider 20 working days per month. Add a method
that calculates the deductions of each employee based on their leave record. In your
main method, create an array of type deduction filled with sample data of all types of
Employees. Finally calculate the total deduction that iterates through the array and
returns the total amount of deductions of all the employees in a month.
8 a) Define a class to maintain bank accounts of customers. The program should
place the code into a try-catch block with multiple catches to check for the validity of
various attributes based on the following criteria.
1. Customer ID must start with a letter and should be followed by three digits.
2. Account number must be of five digits.
3. Initial balance must be above $1000.
Print suitable error matches within the catch block. If any of the criteria mentioned
above is not fulfilled, the program should loop back and let the user enter new data.

b) Modify the previous exercise to include methods for amount deposited and amount
withdrawn. Create your own exception class which will check inside the method for the
amount deposited so that after the deposit, the maximum balance in the account must
not be more than $5000. Also, check inside the method for amount withdrawn so that the
available balance after the withdrawal does not go below $1000. Invoke the defined
methods from your main method and catch the exceptions.
9 a) Write a program to simulate a buzzer. The program should make use of the
Thread class. Create a class named Buzzer that extends the Thread class. Create
an interface named MonitorTime which contains a method setBuzzerTime() . Your
Buzzer class should implement this interface. Override setBuzzerTime() method to
set the buzzer time delay in milliseconds and the number of times the buzzer should
be repeated. Also, include methods blowBuzzer to start the buzzer.
b) Write a Java program which handles Push operation and Pop operation on stack
concurrently.
c) Write a Java program which first generates a set of random numbers and then determines
negative, positive even, positive odd numbers concurrently.
10 a) Write a program that will count the total occurrences of the number ‘10’ in a text file of
strings representing numbers of type int and will show the value of the count on the screen
once the whole file is read. The file contains the following numbers separated by space.
104781034111015610
b) Write a program that reads grades of type double of eight students that the user
provides. The grades lie between 0 and 10. These grades should be written to a
binary file and read from it. The program outputs the highest and lowest grades
achieved by students on the screen. The file contains nothing but numbers of type
double written to the file with writeDouble .
11 The Sieve of Erastothenes is an ancient algorithm that generates prime numbers.
Consider the list of numbers from 2 to 10 as follows:
2345678910
The algorithm starts with the first prime number in the list, which is 2, and then
iterates through the remainder of the list, removing any number that is a multiple of 2
(in this case, 4, 6, 8, and 10), leaving
23579
We then repeat the process with the second prime number in the list, which is 3, and
then iterate through the remainder of the list, removing any number that is a multiple
of 3 (in this case 9), leaving

3
2357
We then repeat starting with each successive prime number, but no elements are
removed because there are no multiples of 5 or 7 (a more efficient implementation of the
algorithm would stop without examining 5 or 7). The numbers that remain in the list are
all prime numbers. Implement this algorithm using an ArrayList of integers that is
initialized to the values from 2 to 100. Your program can iterate numerically through the
ArrayList from index 0 to index size()- 1 to get the current prime number, but should use
an Iterator to scan through the remainder of the list to eliminate the multiples. You can
use the listIterator method to retrieve the iterator starting at a specified index into the
ArrayList . Output all remaining prime numbers to the console.

12 a) Write a JAVA program to paint like paint brush in applet


b) Write a JAVA program that display the x and y position of the cursor movement
using Mouse
c) Write a JAVA program to build a Calculator in AWT

4
WEEK #1
Experiment #1(a):
Question: Write a JAVA program to display default value of all primitive data type of JAVA.
Objective: This experiment gives you to know default values of Java data types

Requirement Analysis:
i. Select any four primitive types
ii. Print the outputs with all primitive types
Algorithm/Procedure/flow chart/class diagram:
Step 1:Start
2: Declare variables without any initial values
3: Print the output of all variables
4: Stop
Expected Input/Output:

5
Experiment #1(b):
Question: Write a case study on public static void main(250 words)
Objective: Student knows complete execution structure of java program from
system prompt
Requirement Analysis:
i) Compare main() of function in C/C++/Java
ii) Analyze that commands executed at system prompt
iii) Analyze the storage keyword static prefixed to main() function
Expected Input/Output:

Text report of 250 words

Experiment #1(c):
Question: Five Bikers Compete in a race such that they drive at a constant
speed which may or may not be the same as the other. To qualify the race, the
speed of a racer must be more than the average speed of all 5 racers. Take as
input the speed of each racer and print back the speed of qualifying racers.
Objective: Student is able to understand variables and arithmetic expressions
Requirement Analysis:
i. Identify variable names and data types
ii. Usage of I/O statements
iii. Using of arithmetic operators
Algorithm/Procedure/flow chart/class diagram:
Step 1:Start
2: Declare five variables for racers speed
3: Input all five racers speed
4: Calculate average
5: Print qualified racers and speed
6: Stop
Expected Input/Output:
Enter speed of 5 racers:
xxx
xxx
xxx
xxx

6
xxx
Average speed is xxx and qualifier racers are below:
xxx
xxx
xxx

WEEK #2
Experiment #2(a):
Question: Write a JAVA program to search for an element in a given list of
elements using binary search mechanism.
Objective: By this experiment we are able to understand declaration and input
process of arrays in Java
Requirement Analysis:
i. Required a sorted array by declaration or input
ii. Basic I/O statements
iii. Looping structures and conditional structures
Algorithm/Procedure/flow chart/class diagram:
Step 1:Start
2: Declare an array with size N
3: Input array elements in sorted order
4: Input searching key
5: Compare and find location of key
6: Stop
Expected Input/Output:
Enter size of array : xxxx
Enter list of elements in sorted order :
xx xx xx xx xx
Enter key value to search : xx
Element xx is found at location xx

Experiment #2(b):
Question: Write a JAVA program to sort for an element in a given list of
elements using bubble sort
Objective: To understand basic sorting technique(bubble) using arrays
in Java Requirement Analysis:
i. Required basic array declaration syntax

7
ii. Basic I/O statements
iii. Looping structures and conditional structures
Algorithm/Procedure/flow chart/class diagram:
Step 1:Start
2: Declare an array with size N
3: Input array elements
4: Use nested loops to compare adjacent elements and swap elements
5: Print array after sorting
6: Stop
Expected Input/Output:
Enter size of array : xx
Input xx no.of elements:
xx xx xxx xx xxx
List of elements after sorting : xx xx xx xx xxx

WEEK #3
Experiment #3(a):
Question: Define a class called CalAge . This class is used to calculate age of
a person from her or his date of birth and the current date. Include a mutator
method that allows the user to enter her or his date of birth and set the value for
current date. Also include a method to return the age in years and months (for
example, 25.5 years) as a double value. Include an additional method to check
if the date of birth entered by the user is a valid one. For example, 30 February
2008 is an invalid date. Embed your class in a test program.
Objective: By this experiment we are able to understand class
definition(members and methods) and object allocation in Java
Requirement Analysis:
i. Required to familiar with access specifiers, data members and methods in class
ii. Syntax and implementing object allocation using new keyword
iii. Driver program with main() to create object and calling methods

Algorithm/Procedure/flow chart/class diagram:


Expected Input/Output:
Enter date of birth : xx/xx/xxxx
Age up to till date: xxx

Experiment #3(b):
Question: Define a class called Journal that could be used to store an entry for
a research paper that will be published. The class should have instance
variables to store the author’s name, title of the paper, and the date of
submission using the Date class from this chapter. Add a constructor to the
class that allows the user of the class to set all instance variables. Also add a
method, displayDetails , that outputs all the instance variables, and another
method called getSubmissionDetails that returns the title of the paper, with the
first letter of each word capitalized. Test your class from the main method.

Objective: By this experiment we are able to understand class instance


variables initialization using constructors.
Requirement Analysis:
i. Required default and parameterized constructors
ii. String methods to split into words
iii. Required to know Character operations to capitalize.
Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter author name :
Enter title of the paper :
Enter date of submission(dd/mm/yyyy) : xx/xx/xxxx
Journal paper details:
Author: xxxxxx Title: Xxx Xx Xxxxxx Xxxxxxxx Xxxx Date: xx/xx/xxxx

9
WEEK #4
Experiment #4:
Question: Define a class called BookStore to maintain the record of books sold.
The store contains three categories of books i.e. “Kids”, “Engineering”, and “Story”.
The following details should be maintained for each book. Book category, Author,
Title, Publisher, Selling price of the book, Quantity. Create a constructor that
initializes the Store ID and the rest of the details of the book mentioned above.
Include a method named trackSalesStatus that will display the total number of
books sold by the store (add a static variable that tracks the total number of books
sold). Also, include a method to display the quantity available corresponding to
each Book ID. Write a main method to test your class.

Objective: By this experiment we are able to understand class level global


variable called static
Requirement Analysis:
i. Required to familiar with access specifiers, data members and methods in class
ii. Required to be familiarized with static instance variable as global
iii. Driver program with main() to create object and calling methods

Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter no.of books to be created : xxx
Enter n_th Book details:
ID:
Category:
Author:
Title:

10
Publisher:
Price:
Quantity:
Enter n_th Book details:
… … …

Total no.of books sold: xxxxx


Enter book ID to get details: xxx
Book <id> is found and available quantity : xxxx

WEEK #5
Experiment #5(a):
Question: You are running a courier agency. The weight of a parcel determines
the number of stamps that will be needed to send that parcel. For each
kilogram, a stamp of $2 is needed. Create a class to accept the weight of five
parcels in floating-point values. Also, the courier company charges an additional
rate depending on where the courier has to be delivered. The charges are $20
for delivery within the city of posting, and $40 for delivery anywhere else in the
country. Write a computer program to calculate and display the total cost of
each parcel depending on the weight and delivery location of the parcel.
Objective: By this experiment, student is able to understand arrays and its processing

Requirement Analysis:
i. Required to familiarized with array declaration and allocation
ii. Required to have knowledge in accessing array through index positions
iii. Driver program with main() to create object and calling methods
Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter no.of courier parcels: xx
Enter xx Courier weight in kgs:
Enter xx Courier location(local/non-local):
..

11

Parcel# total cost(Rs):
Parcel# total cost(Rs):
….

Experiment #5(b):
Question:Write a program that takes as input a set of 5 numbers from the keyboard
into an array of type int[] . Create another array that will also type int. The output is to
be a two-column list. The first column is a list of the distinct array elements; the second
column is the count of the number of occurrences of each element.
Objective:
Requirement Analysis:
i. Required to familiarized with array declaration and allocation
ii. Student should be familiarized with 2-D arrays to two column data
iii. Driver program with main() to create object and calling methods
Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter 5 array of numbers: 3 4 1 3 4 7 1 9 1
Count of distinct elements:
3 2
4 2
1 3
7 1
9 1

WEEK #6
Experiment #6:
Question: Define a class named Person that contains two instance variables of type String

that stores the first name and last name of a person and appropriate accessor and mutator

methods. Also create a method named displayDetails that outputs the details of a person.

Next, define a class named Student that is derived from - Person , the

12
constructor for which should receive first name and last name from the class
Student and also assigns values to student id, course, and teacher name. This
class should redefine the displayDetails method to person details as well as
details of a student. Include appropriate constructor(s). Define a class named
Teacher that is derived from Person. This class should contain instance
variables for the subject name and salary. Include appropriate constructor(s).
Finally, redefine the displayDetails method to include all teacher information in
the printout. Create a main method that creates at least two student objects and
two teacher objects with different values and calls displayDetails for each.
Objective: By this experiment, student is able to understand inheritance and
overriding of methods
Requirement Analysis:
i. Required to familiarized with extending of classes(inheritance)
ii. Required to have knowledge in methods overriding
iii. Driver program with main() to create object and calling methods
Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
st
Enter 1 student details:
Student ID:
Course:
Teacher Name:

nd
Enter 2 student details:
Student ID:
Course:
13
Teacher Name:
st
Enter 1 teacher details:
Subject Name:
Salary:
nd
Enter 2 teacher details:
Subject Name:
Salary:
Student Details:
xxx xxx xxxxx
xxx xxx xxxxx
Teacher Details:
xxx xxx xxxxx

WEEK #7
Experiment #7:
Question:
Create a class named Employee that can be used to calculate the salaries of different
employees. The Employee class should keep a track of the employee ID, name,
department, salary, and designation with appropriate accessor and mutator methods.
Also create an equals() method that overrides Object’s equals() method, where
employees can check if their designation is identical. Next, create two additional
classes named Manager and Clerk that are derived from Employee . Create an
overridden method name addBonus that returns the salary of the employee after
adding up the bonus. There is a default bonus of $200/month. Managers have a bonus
of $300/month and clerks have a bonus of $100/month. Finally create a display method
to print the details of the employee. You may assume the initial salary of an employee
and other necessary values. Test your classes from a main method.

Objective: By this experiment, student is able to understand inheritance and


overriding of methods along with predefined method equal() overriding.
Requirement Analysis:
i. Required to familiarized with extending of classes(inheritance)
ii. Required to have knowledge in methods overriding
iii. Driver program with main() to create object and calling methods using

object references and equal() method

14
Algorithm/Procedure/flow chart/class diagram:

Extend the previous problem to calculate the salary deductions based on the number of days an employee is
on leave. Consider 20 working days per month. Add a method that calculates the deductions of each
employee based on their leave record. In your main method, create an array of type deduction filled with
sample data of all types of Employees. Finally calculate the total deduction that iterates through the array and
returns the total amount of deductions of all the employees in a month.

Expected Input/Output:
Enter employee details:
ID: xxx
Name : xxxxxx
Designation: Manager
Department: Admin
Salary: 4590

Enter employee Details:


ID : xxxx
Name: xxxxxx
Designation: Clerk
Department: Admin

Enter Employee details:


ID : xxxx
Name: xxxxxx
Designation: Manager
Department: Finance

Does employee1 and employee2 are equal: False


Does employee1 and employee3 are equal: True

Employee1 salary after adding Bonus: xxxx


Employee2 salary after adding Bonus: xxxx

Extension of above code:


b) Extend the previous problem to calculate the salary deductions based on the number of days an
employee is on leave. Consider 20 working days per month. Add a method that calculates the deductions
of each employee based on their leave record. In your main method, create an array of type deduction
filled with sample data of all types of Employees. Finally calculate the total deduction that iterates through
the array and returns the total amount of deductions of all the employees in a month.
Expected Input/Output:
Enter no.of employees : xxx
Enter employee1 details:
…..
….
No.of days leave: xxx
Enter employee2 details:
…..
15
….
No.of days leave: xxx
…….
Enter employeeN details:
…..
….
No.of days leave: xxx

Total deductions from all employees : xxxxx

WEEK #8
Experiment #8(a):
Question:
a) Define a class to maintain bank accounts of customers. The program should
place the code into a try-catch block with multiple catches to check for the
validity of various attributes based on the following criteria.
1. Customer ID must start with a letter and should be followed by three digits.
2. Account number must be of five digits.
3. Initial balance must be above $1000.
Print suitable error matches within the catch block. If any of the criteria mentioned
above is not fulfilled, the program should loop back and let the user enter new data.

Objective: By this experiment, student is able to understand exception handling


keywords try/catch
Requirement Analysis:
i. Required to familiarized with try/catch keywords
ii. Required to have knowledge in exception throwing using throw keyword
iii. Driver program with main() to create AccountDetails object and call methods

Algorithm/Procedure/flow chart/class diagram:

Note: readAccountDetails() method takes each input, while taking each input validate

with suitable try/catch block. And loop back to same input field until correct input rules

16
Experiment #8(b):
Question:
Modify the previous exercise to include methods for amount deposited and amount
withdrawn. Create your own exception class which will check inside the method for the
amount deposited so that after the deposit, the maximum balance in the account must
not be more than $5000. Also, check inside the method for amount withdrawn so that
the available balance after the withdrawal does not go below $1000. Invoke the defined
methods from your main method and catch the exceptions.

Objective: By this experiment, student is able to create user defined exceptions


and overriding toString() method
Requirement Analysis:
i. Required to familiarized with try/catch keywords
ii. Required to have knowledge in extending Exception super class
iii. You must override toString() method

Algorithm/Procedure/flow chart/class diagram:

Expected input/output:
….Continuation to 8A output……
Enter amount to deposit : xxxxx
Sorry, account balance is more than 5000, you cannot deposit xxxx !!!
Enter amount to deposit : xxxxx
Yes, your amount is deposited, current balance : xxxxx
Enter amount to withdraw : xxxxx
Sorry, account balance is less than 1000, you cannot withdraw xxxx!!!
Enter amount to withdraw : xxxxx
Yes, your amount xxxx is with, current balance : xxxxx

17
WEEK #9
Experiment #9(a):
Question:Write a program to simulate a buzzer. The program should make use of the
Thread class. Create a class named Buzzer that extends the Thread class. Create an
interface named MonitorTime which contains a method setBuzzerTime() . Your Buzzer
class should implement this interface. Override setBuzzerTime() method to set the
buzzer time delay in milliseconds and the number of times the buzzer should be
repeated. Also, include methods blowBuzzer to start the buzzer.

Objective: By this experiment, student is able to create multi threaded programming using

Thread as super class. He/She is able implement user defined interface methods.

Requirement Analysis:
i) He/She must familiar with extending Thread class and overriding run() method
ii) You are required to define an interface MonitorTime with setBuzzerTime() ie.,
to be implemented by sub classes
iii) Finally you must be familiar with writing other methods like blowBuzzer(),
main() and calling start() method

Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter number of times buzzer should be repeated : 3
Set Buzzer time delay in seconds : 1
1 Buzzer started…
2 Buzzer started…
3 Buzzer started…

18
Experiment #9(b):
Question: Write a Java program which handles Push operation and Pop
operation on stack concurrently

Objective: By this experiment, student is able to create multi threaded programming


using Thread/Runnable. He/She is able implement stack operations push() and pop()
Requirement Analysis:
i) Create a stack of integers with N size
ii) Extend Thread class or implement Runnable interface
iii) Write two methods push() and pop() and call them using run() method
concurrently with time delay of 1sec

Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter stack size: 3
3 random elements are pushed and popped below:
Pushed 45
Popped 45
Pushed 34
Pushed 90
Popped 90
Popped 34

Experiment #9(c):
Question: Write a Java program which first generates a set of random numbers
and then determines negative, positive even, positive odd numbers concurrently

Objective: By this experiment, student is able to create multi threaded


programming using Thread/Runnable. He/She is able understand Random class.

19
Requirement Analysis:
i) Create a class with N no.of as integer elements
ii) Define main() method for Random class object and generate numbers
between min to max
iii) Print them in positive – Even/Odd and negative categories

Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter number of elements to be generated : 5
Enter minimum : -6
Enter maximum: 8
5 – Positive odd
6 – Positive even
-5 – Negative
-3 – Negative
3 – Positive odd

WEEK #10
Experiment #10(a):
Question:Write a program that will count the total occurrences of the number
‘10’ in a text file of strings representing numbers of type int and will show the
value of the count on the screen once the whole file is read. The file contains
the following numbers separated by space.
104781034111015610

Objective: By this experiment, student is able to understand reading of text file


using suitable file streams

20
Requirement Analysis:
i) A data file with no.of integers separated by space
ii) Use built-in package java.io.*
iii) Create file stream objects and read the data

Algorithm/Procedure/flow chart/class diagram:

Expected Input/Output:
Enter filename to read : sample.txt
Total number of 10’s : 04

Experiment #10(b):
Question: Write a program that reads grades of type double of eight students
that the user provides. The grades lie between 0 and 10. These grades should
be written to a binary file and read from it. The program outputs the highest and
lowest grades achieved by students on the screen. The file contains nothing but
numbers of type double written to the file with writeDouble.

Objective: By this experiment, student is able to understand writing and reading


binary files

Requirement Analysis:
i) Open data file with binary writing and reading mode
ii) Create Scanner object and read 8 inputs from keyboard
iii) Write and read them using binary mode files

Algorithm/Procedure/flow chart/class diagram:


- An executable main() program with all required code

21
Expected input/output:
Enter 8 grades of 8 students:
4.5
7.8
9.3
4.6
6.67
8.23
8.44
6.23
Enter data file to write : grades.bin
Data is written to grades.bin
Reading from grades.bin
Lowest grade : 4.5
Highest grade : 9.3

WEEK #11
Experiment #11):
Question: The Sieve of Erastothenes is an ancient algorithm that generates
prime numbers. Consider the list of numbers from 2 to 10 as follows:
2345678910
The algorithm starts with the first prime number in the list, which is 2, and then
iterates through the remainder of the list, removing any number that is a multiple
of 2 (in this case, 4, 6, 8, and 10), leaving
23579
We then repeat the process with the second prime number in the list, which is 3,
and then iterate through the remainder of the list, removing any number that is a
multiple of 3 (in this case 9), leaving

2357
We then repeat starting with each successive prime number, but no elements are removed

because there are no multiples of 5 or 7 (a more efficient implementation of the algorithm

would stop without examining 5 or 7). The numbers that remain in the list are all prime

numbers. Implement this algorithm using an ArrayList of integers that is initialized to the

values from 2 to 100. Your program can iterate numerically through the ArrayList from

index 0 to index size()-1 to get the current prime number, but should use

22
an Iterator to scan through the remainder of the list to eliminate the multiples. You
can use the listIterator method to retrieve the iterator starting at a specified index
into the ArrayList. Output all remaining prime numbers to the console.

Objective: By this experiment, student is able to understand collections ie.,


ArrayList and Iterator

Requirement Analysis:
i) Use built-in package java.io.ArrayList collection
ii) Use Iterator object to traverse ArrayList
iii) Finally display list of prime numbers

Algorithm/Procedure/flow chart/class diagram:

Expected input/output:
Enter N as maximum prime number : 10
Generating prime numbers….
Finally prime numbers : 2 3 5 7

23
WEEK #12
Experiment #12(a):
Question: Write a JAVA program to paint like paint brush in applet

Objective: By this experiment, student is able to understand Applet programming

Requirement Analysis:
i) Use built-in package java.applet.*, java.awt.event.*, java.awt.*
ii) Use various mouse listeners to draw objects using simple pixel

Algorithm/Procedure/flow chart/class diagram:

Experiment #12(b):
Question: Write a JAVA program that display the x and y position of the cursor
movement using Mouse

Objective: By this experiment, student is able to understand Event handling in


AWT/Applets

Requirement Analysis:
i) Use built-in package java.applet.*, java.awt.event.*, java.awt.*
ii) Use various mouse listeners to detect mouse cursor

Algorithm/Procedure/flow chart/class diagram:

Experiment #12(c):
Question: Write a JAVA program to build a Calculator in AWT

24
Objective: By this experiment, student is able to understand Event handling in
AWT and various layout managers
Requirement Analysis:
i) Use built-in package java.applet.*, java.awt.event.*, java.awt.*
ii) Use various layout managers and event handling methods

25

You might also like