Java All 5 Practical Questions with Answers
Java All 5 Practical Questions with Answers
Program 1
Write a Java program to handle rational numbers. The program should:
1. Acceptinputforthenumeratoranddenominatorofarationalnumber.
2. Displaytheoriginalrationalnumber.
3. Simplifytherationalnumbertoitsreducedform.
4. Displaythereducedform.
Example:
NUMERATOR=60
DENOMINATOR=20
BEFORE SIMPLIFICATION=60/20
Reduced form=3/1
ANSWER
import java.util.Scanner;
public class RationalNumber {
private int numerator;
private int denominator;
Java Practical 1
}
System.out.print("NUMERATOR = ");
int numerator = scanner.nextInt();
System.out.print("DENOMINATOR = ");
int denominator = scanner.nextInt();
scanner.close();
}
}
Java Practical 2
Output:
Experiment 2
Develop a user-defined package in Java named Date, which consists of a class
named CurrentDate. This package should contain methods to display the
current
date in the format "day/month/year" and the current time in the format
"hour:minute
AM/PM".
Write an application program that imports this package and uses the
CurrentDate
class to display the current date and time.
ANSWER
Here's how you can create the package and the application program concisely:
package Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Java Practical 3
attern("dd/MM/yyyy")));
}
import Date.CurrentDate;
Output:
Experiment 3
Java Practical 4
methods. Instantiate objects of both classes and demonstrate inheritance by
accessing attributes and methods of both classes.
class Base{
int a=2;
public void baseMethod(){
System.out.println("This is from base class");
}
public void baseMethodforDerivedClass(){
System.out.println("This is from base class");
}
}
class derived extends Base{
int b=3;
void derivedMethod(){
System.out.println("This is from derived class");
}
}
public class p3_Inheritance {
public static void main(String[] args) {
Base b1 = new Base();
derived d1 = new derived();
b1.baseMethod();
System.out.println("This is the attribute of base c
lass: "+ b1.a);
d1.derivedMethod();
d1.baseMethodforDerivedClass();
System.out.println(d1.b);
Output:
Java Practical 5
Experiment 4
Write a Java program to create a scientific calculator using Swing. The
calculator
should have functionalities for basic arithmetic operations (+, -, *, /),
trigonometric
functions (sin, cos, tan), and square root. The GUI should display the
calculation as
it progresses and should include necessary buttons for user interaction.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public ScientificCalculator() {
setLayout(new BorderLayout());
Java Practical 6
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
add(panel, BorderLayout.CENTER);
setTitle("Scientific Calculator");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Java Practical 7
if (e.getSource() == numButtons[i]) {
display.setText(display.getText() + i);
}
}
if (e.getSource() == addButton) { operator = "+"; n
um1 = readDisplay(); clearDisplay(); }
if (e.getSource() == subButton) { operator = "-"; n
um1 = readDisplay(); clearDisplay(); }
if (e.getSource() == mulButton) { operator = "*"; n
um1 = readDisplay(); clearDisplay(); }
if (e.getSource() == divButton) { operator = "/"; n
um1 = readDisplay(); clearDisplay(); }
if (e.getSource() == sinButton) { display.setText(S
tring.valueOf(Math.sin(Math.toRadians(readDisplay())))); }
if (e.getSource() == cosButton) { display.setText(S
tring.valueOf(Math.cos(Math.toRadians(readDisplay())))); }
if (e.getSource() == tanButton) { display.setText(S
tring.valueOf(Math.tan(Math.toRadians(readDisplay())))); }
if (e.getSource() == sqrtButton) { display.setText
(String.valueOf(Math.sqrt(readDisplay()))); }
if (e.getSource() == eqButton) {
num2 = readDisplay();
switch (operator) {
case "+": display.setText(String.valueOf(nu
m1 + num2)); break;
case "-": display.setText(String.valueOf(nu
m1 - num2)); break;
case "*": display.setText(String.valueOf(nu
m1 * num2)); break;
case "/": display.setText(String.valueOf(nu
m1 / num2)); break;
}
}
if (e.getSource() == clrButton) { clearDisplay(); }
}
Java Practical 8
}
Output:
Experiment 5
Java Practical 9
The
task involves implementing two functions:
1. car: This function should return the first element of the list.
2. cdr: This function should return the rest of the list after removing the first
element.
LispList.java
import java.util.ArrayList;
import java.util.List;
@Override
public String toString() {
return list.toString();
}
Java Practical 10
public static void main(String[] args) {
LispList list = new LispList(1, 2, 3, 4, 5);
System.out.println("List: " + list);
System.out.println("car: " + list.car());
System.out.println("cdr: " + list.cdr());
}
}
Output:
Java Practical 11