LAB ASSIGNMENT 8
Write a Java Applet program with three interconnected pages. The first page should take the first
number as input and have an “OK” button. The second page should take the next number and
have “Previous” and “Next” buttons. The third page should display the sum of both numbers and
have a “Previous” button.
import [Link].*;
import [Link].*;
import [Link].*;
public class ThreePageApplet extends JFrame implements ActionListener {
int page = 1;
JTextField t1, t2;
JButton ok, prev, next;
int n1, n2;
JLabel l;
public ThreePageApplet() {
setTitle("Three Page Applet (Swing Version)");
setSize(400, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center window on screen
initPage1();
setVisible(true);
}
// --- Page 1 ---
private void initPage1() {
getContentPane().removeAll();
l = new JLabel("Enter first number:");
[Link](50, 50, 150, 30);
add(l);
t1 = new JTextField();
[Link](210, 50, 100, 30);
add(t1);
ok = new JButton("OK");
[Link](140, 100, 80, 30);
add(ok);
[Link](this);
Himanshi Jain 48 23124040
repaint();
revalidate();
}
// --- Page 2 ---
private void initPage2() {
getContentPane().removeAll();
l = new JLabel("Enter next number:");
[Link](50, 50, 150, 30);
add(l);
t2 = new JTextField();
[Link](210, 50, 100, 30);
add(t2);
prev = new JButton("Previous");
[Link](80, 100, 100, 30);
add(prev);
[Link](this);
next = new JButton("Next");
[Link](200, 100, 80, 30);
add(next);
[Link](this);
repaint();
revalidate();
}
// --- Page 3 ---
private void initPage3() {
getContentPane().removeAll();
int sum = n1 + n2;
l = new JLabel("Sum = " + sum);
[Link](150, 60, 150, 30);
add(l);
prev = new JButton("Previous");
[Link](150, 100, 100, 30);
add(prev);
Himanshi Jain 49 23124040
[Link](this);
repaint();
revalidate();
}
// --- Button Actions ---
public void actionPerformed(ActionEvent e) {
if (page == 1 && [Link]() == ok) {
try {
n1 = [Link]([Link]());
page = 2;
initPage2();
} catch (NumberFormatException ex) {
[Link](this, "Please enter a valid number!");
}
}
else if (page == 2 && [Link]() == next) {
try {
n2 = [Link]([Link]());
page = 3;
initPage3();
} catch (NumberFormatException ex) {
[Link](this, "Please enter a valid number!");
}
}
else if ([Link]() == prev) {
if (page == 3) {
page = 2;
initPage2();
} else if (page == 2) {
page = 1;
initPage1();
}
}
}
// --- Main Method (to run in Terminal) ---
public static void main(String[] args) {
[Link](() -> new ThreePageApplet());
}
}
Himanshi Jain 50 23124040
Himanshi Jain 51 23124040
[Link] a Java Swing program to draw a circle. Inside the circle, at the top-middle
position, draw a sun whose center is a dot and with rays extending outward. In the
bottom half of the circle, draw mountain-like shapes using zigzag lines such that the sun
appears above the mountains and in their center
import [Link].*;
import [Link].*;
public class SunAndMountainScene extends JPanel {
@Override
protected void paintComponent(Graphics g) {
[Link](g);
setBackground([Link]);
Graphics2D g2 = (Graphics2D) g;
[Link](new BasicStroke(2));
int width = getWidth();
int height = getHeight();
// Draw main circle
int circleDiameter = [Link](width, height) - 100;
int circleX = (width - circleDiameter) / 2;
int circleY = (height - circleDiameter) / 2;
[Link]([Link]);
[Link](circleX, circleY, circleDiameter, circleDiameter);
// Circle center
int centerX = circleX + circleDiameter / 2;
int centerY = circleY + circleDiameter / 2;
// Draw mountains (bottom half)
[Link](new Color(100, 60, 20)); // brownish color
int baseY = centerY + circleDiameter / 4;
int startX = circleX + 30;
int endX = circleX + circleDiameter - 30;
int step = 50;
int peak = 70;
int prevX = startX, prevY = baseY;
boolean up = true;
for (int x = startX + step; x <= endX; x += step) {
int newY = up ? baseY - peak : baseY;
Himanshi Jain 52 23124040
[Link](prevX, prevY, x, newY);
prevX = x;
prevY = newY;
up = !up;
}
// Draw sun above mountains (upper side of circle)
int sunRadius = 25;
int sunCenterY = centerY - circleDiameter / 4; // move sun to upper half
// Draw sun rays
[Link]([Link]);
int rayLength = 45;
for (int angle = 0; angle < 360; angle += 30) {
double rad = [Link](angle);
int x1 = (int) (centerX + sunRadius * [Link](rad));
int y1 = (int) (sunCenterY + sunRadius * [Link](rad));
int x2 = (int) (centerX + (sunRadius + rayLength) * [Link](rad));
int y2 = (int) (sunCenterY + (sunRadius + rayLength) * [Link](rad));
[Link](x1, y1, x2, y2);
}
// Draw sun center as a small dot
[Link]([Link]);
[Link](centerX - 3, sunCenterY - 3, 6, 6);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Sun Above Mountains Scene");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](600, 600);
[Link](new SunAndMountainScene());
[Link](true);
}
}
Himanshi Jain 53 23124040
Himanshi Jain 54 23124040
Lab Assignment 9
Q1. Write a Java program to create a GUI with two tabs:
i. Accept two numbers as input from the user.
ii. Perform addition of the two numbers and display the result.
iii. Provide a button to navigate to the second tab.
B. Use the entered in the first tab.
ii. Perform division of those numbers and display the result.
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="TwoTabApplet" width="400" height="200"></applet>
*/
public class TwoTabApplet extends Applet implements ActionListener {
CardLayout cl = new CardLayout();
Panel cards = new Panel();
TextField n1, n2, addRes, divRes;
Button addBtn, nextBtn, divBtn;
int num1, num2;
public void init() {
[Link](cl);
Panel tab1 = new Panel(new GridLayout(4, 2, 5, 5));
n1 = new TextField(); n2 = new TextField(); addRes = new TextField(); [Link](false);
addBtn = new Button("Add"); nextBtn = new Button("Go to Tab 2");
[Link](new Label("Number 1:")); [Link](n1);
[Link](new Label("Number 2:")); [Link](n2);
[Link](addBtn); [Link](addRes);
[Link](nextBtn);
[Link](tab1, "Tab1");
Panel tab2 = new Panel(new GridLayout(2, 2, 5, 5));
divRes = new TextField(); [Link](false); divBtn = new Button("Divide");
[Link](new Label("Division (num1/num2):")); [Link](divRes);
[Link](divBtn);
[Link](tab2, "Tab2");
Himanshi Jain 55 23124040
add(cards);
[Link](this);
[Link](e -> [Link](cards, "Tab2"));
[Link](this);
}
public void actionPerformed(ActionEvent e) {
try {
num1 = [Link]([Link]());
num2 = [Link]([Link]());
if ([Link]() == addBtn) {
[Link]([Link](num1 + num2));
}
if ([Link]() == divBtn) {
[Link](num2 != 0 ? [Link]((double) num1 / num2) : "Cannot divide by zero");
}
} catch (NumberFormatException ex) {
[Link]("Invalid"); [Link]("Invalid");
}
}
}
Himanshi Jain 56 23124040
Q2.Q2. Draw the Pic_G2_Q2 diagram using java Applet.
import [Link].*;
import [Link].*;
/*
<applet code="PicG2Q2" width="400" height="300"></applet>
*/
public class PicG2Q2 extends Applet {
public void paint(Graphics g) {
setBackground([Link]);
[Link]([Link]);
[Link](50, 150, 300, 100);
[Link]([Link]);
[Link](150, 50, 100, 100);
[Link]([Link]);
[Link](165, 125, 10, 10);
}
}
Himanshi Jain 57 23124040
Himanshi Jain 58 23124040
LAB ASSIGNMENT 10
1. Reverse Words Alphabetically: Read a sentence, use StringTokenizer to extract words, and print
them in sorted (alphabetical) order.
import [Link].*;
import [Link].*;
public class Sorting{
public static void main(String args[]) throws IOException {
DataInputStream dis = new DataInputStream([Link]);
String str = [Link]();
StringTokenizer st = new StringTokenizer(str, " ");
int n = [Link]();
String arr[] = new String[n];
int i = 0;
while ([Link]()) {
arr[i] = [Link]();
i++;
}
[Link](arr);
[Link]("Sorted Tokens:");
for (String s : arr) {
[Link](s);
}
}
}
Himanshi Jain 59 23124040
2. Find Duplicate Words: Read a sentence, split it using StringTokenizer, and identify which words
appear more than once and remove.
import [Link].*;
import [Link];
public class DuplicateWords {
public static void main(String args[]) throws IOException {
DataInputStream dis = new DataInputStream([Link]);
String str = [Link]();
StringTokenizer st = new StringTokenizer(str, " ");
int n = [Link]();
String words[] = new String[n];
int i = 0;
while ([Link]()) {
words[i] = [Link]();
i++;
}
[Link]("After removing duplicates:");
for (i = 0; i < [Link]; i++) {
if (words[i] == null)
continue;
for (int j = i + 1; j < [Link]; j++) {
if (words[i].equals(words[j])) {
words[j] = null;
}
}
[Link](words[i]);
Himanshi Jain 60 23124040
}
}
}
3. Capitalize First and Last Letter of Each Word: Read a line and output it with each word’s
first letter capitalized.
import [Link].*;
public class q3 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link]();
String[] words = [Link](" ");
StringBuilder sb = new StringBuilder();
for (String w : words) {
if ([Link]() == 1) {
[Link]([Link]());
} else {
String newWord =
[Link]([Link](0)) +
[Link](1, [Link]()-1) +
[Link]([Link]([Link]()-1));
[Link](newWord);
}
[Link](" ");
}
[Link]([Link]().trim());
Himanshi Jain 61 23124040
}
}
4. Word Replacement: Read a line and replace all occurrences of one word (given by the user)
with another.
import [Link].*;
import [Link];
public class Replace2{
public static void main(String args[]) throws IOException {
DataInputStream dis = new DataInputStream([Link]);
String str = [Link]();
StringTokenizer st=new StringTokenizer(str," ");
int n=[Link]();
String arr[]=new String[n];
int i=0;
while([Link]())
{
arr[i]=[Link]();
i++;
}
[Link]("Enter new word");
String new_word=[Link]();
[Link]("Enter old word");
String old_word=[Link]();
for(i=0;i<n;i++)
{
Himanshi Jain 62 23124040
if(arr[i].equals(old_word))
{
arr[i]=new_word;
}
[Link](arr[i]);
}
Himanshi Jain 63 23124040