java.exam
java.exam
1. A company pays its employees on a weekly basis. The employees are of two types:
Salaried employees are paid a fixed weekly salary, hourly employees are paid by the hour and
receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of
40 hours. Write an application that performs its payroll calculations polymorphically.
//Employee.java
firstName = f;
lastName = l;
// SalariedEmployee.java
super(f, l);
salary = s;
return salary;
}
public String toString() {
String n = super.toString();
return info;
// HourlyEmployee.java
super(f, l);
wage = w;
hours = h;
if ( hours <= 40 )
else
String n = super.toString();
return info;
//PayRollSystem.java
System.out.println(e1.toString());
System.out.println(e2.toString());
Output:
Salary: 1538.0
Wage: 25.0
Hours: 42.0
2. Write a program that demonstrate the use of interfaces for different animals. Create three
interfaces: Walkable, Swimmable and Flyable. Each interface should declare a method that
describe the animal’s ability. Create two animal classes that implement any or all of these
interfaces and display the result.
void walk();
}
void swim();
void fly();
duck1.walk();
System.out.println();
duck2.swim();
fish.swim();
Output:
Chapter-14
2. Write a GUI application that display a window containing one textfield and two checkboxes.
Users should be able to change the font style of the textfield by activating/deactivating those
checkboxes. The output window would look like the following figure.
Program:
import java.awt.*;
import javax.swing.*;
JTextField text;
public CheckBoxFrame() {
super("JCheckbox Test");
setLayout(new FlowLayout());
text.setFont(defaultFont);
add(text);
add(bold);
bold.addItemListener(
e ->{
ChangeStyle();
);
add(itallic);
itallic.addItemListener(
e ->{
ChangeStyle();
);
void ChangeStyle() {
}else if(bold.isSelected()){
text.setFont(new Font("Serif",Font.BOLD,20));
}else if(itallic.isSelected()) {
text.setFont(new Font("Serif",Font.ITALIC,20));
frame.setSize(300,150);
frame.setVisible(true);
5. Implement a GUI window that contains a JTextArea. The program should detect three inputs
from user: mouse position, mouse button press and keyboard button press. Update JTextArea’s
text based on user inputs to display the information. The GUI window would look like the
following figure.
// MouseTrackerFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public MouseTrackerFrame(){
stats.setFont(new Font("Serif",3,20));
stats.setEditable(false);
add(stats);
stats.addMouseListener(handler);
stats.addMouseMotionListener(handler);
stats.addKeyListener(new KeyHandler());
UpdateStats();
}
private class KeyHandler extends KeyAdapter{
UpdateStats();
UpdateStats();
UpdateStats();
void UpdateStats() {
String s = String.format("%s\n%s\n%s",mousePos,mouseButton,keyPressed);
stats.setText(s);
//MouseTrackerTest.java
frame.setSize(400, 200);
frame.setVisible(true);
}
6. Create a java application that display a GUI window containing two JTextArea and one
JButton. User should be able to copy the selected text from first JTextArea to the second
JTextArea when the button is clicked. The output window would look like the following figure.
// TextAreaFrame.java
import java.awt.event.*;
import javax.swing.*;
public TextAreaFrame(){
super("TextArea Demo");
box.add(copyJButton);
copyJButton.addActionListener(
new ActionListener(){
textArea2.setText(textArea1.getSelectedText());
});
textArea2.setEditable(false);
box.add(new JScrollPane(textArea2));
add(box);
//TextAreaDemo.java
frame.setSize(425, 200);
frame.setVisible(true);
Chapter-20
1. Write a java application that sort the following product list and display the sorted results.
Create a class called Item that has three instance variables: ID, itemName, and price. Use a
collection data structure to store a list of Item objects and sort them based on their unit price
in descending order.
Program:
//Item.java
ID = id;
productName = name;
price = p;
// PriceComparator.java
import java.util.*;
int result;
result = -1;
}else if(a.price < b.price){
result = 1;
}else {
result = 0;
return result;
//InvoiceDisplay.java
import java.util.*;
System.out.print("Invoice list\n");
DisplayList();
Collections.sort(products,new PriceComparator());
DisplayList();
for(Item i : products) {
System.out.printf("%3d %-20s %.2f \n",i.ID,i.productName,i.price);
Output:
Invoice list
ID Product Price
ID Product Price
#Server Config
host=localhost
level=DEBUG
password=1234
port=800
username=root
Program:
import java.io.*;
import java.util.*;
public class PropertiesTest {
public static void main(String[] args) {
Properties table = new Properties();
table.setProperty("port","800");
table.setProperty("host","localhost");
table.setProperty("username","root");
table.setProperty("password","1234");
table.setProperty("level","DEBUG");
saveFile(table);
}
if(option == 2) {
loadFile(table);
}
}
public static void loadFile(Properties table) {
try {
FileInputStream file = new FileInputStream("props.ini");
table.load(file);
file.close();
System.out.print("\nFile Loaded!");
displayTable(table);
}catch(IOException e) {
System.out.print("\nFailed to load file!");
e.printStackTrace();
}
}
public static void saveFile(Properties table) {
try {
displayTable(table);
FileOutputStream file = new FileOutputStream("props.ini");
table.store(file, "Server Config");
file.close();
System.out.print("\nFile Saved!");
}catch(IOException e) {
System.out.print("\nFailed to save file!");
e.printStackTrace();
}
}
public static void displayTable(Properties table) {
System.out.print("\n----Properties Table----\n");
for(Object key : table.keySet()) {
String value = table.getProperty((String)key);
System.out.printf("%-10s %-10s\n",key, value);
}
}
}
Chapter-25
Chapter-28
1. Develop a Java application that connect to a database and print the table using the
following information.
url: jdbc:mysql://localhost/library
username: root
password: root
i name
d
1 laptop
2 smartphone
3 wireless
mouse
Program:
import java.sql.*;
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from hein");
System.out.print("\n----------------------------------\n");
System.out.print("\n----------------------------------\n");
while(rs.next()) {
System.out.printf( "%-6s\t",rs.getObject(i));
System.out.println();
}catch(SQLException e) {
System.out.println("Error "+e);