java_3LabCycle_fixed
java_3LabCycle_fixed
PROGRAM NO:18
AIM: Create a file and write some contents into a file. Read the contents and display it.
PROGRAM:
import java.io.*; class
FileOperations {
public static void main(String[] args) throws IOException {
FileOutputStream fout = new FileOutputStream("myfile.txt");
DataInputStream din = new DataInputStream(System.in);
System.out.println("Enter the content (press # to quit):");
char ch;
while ((ch = (char) din.read()) != '#') {
fout.write(ch);
}
fout.close();
int content;
while ((content = fin.read()) != -1) {
System.out.print((char) content);
}
fin.close();
}
}
OUTPUT:
DATE:13/05/2024
PROGRAM NO:19
PROGRAM:
import java.io.*;
class Copy {
public static void main(String[] args) throws IOException {
fout.close();
}
}
OUTPUT:
DATE:13/05/2024
PROGRAM NO:20
2. Olympics symbol
PROGRAM:
1.
import java.applet.Applet;
import java.awt.*;
/*
<applet code="TrafficSignal.class" width="300" height="400">
</applet>
*/
public class TrafficSignal extends Applet {
public void paint(Graphics g) {
setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.fillRect(100, 50, 100, 300);
g.setColor(Color.BLACK);
g.fillRect(140, 350, 20, 150);
g.setColor(Color.RED);
g.fillOval(125, 75, 50, 50);
g.setColor(Color.YELLOW);
g.fillOval(125, 175, 50, 50);
g.setColor(Color.GREEN);
g.fillOval(125, 275, 50, 50);
}
}
2.
import java.applet.Applet;
import java.awt.*;
/*
<applet code="OlympicsSymbol.class" width="400" height="300">
</applet>
*/
public class OlympicsSymbol extends Applet {
public void paint(Graphics g) {
setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.drawOval(100, 100, 50, 50);
g.setColor(Color.BLACK);
g.drawOval(160, 100, 50, 50);
g.setColor(Color.RED);
g.drawOval(220, 100, 50, 50);
g.setColor(darkYellow);
g.drawOval(130, 140, 50, 50);
g.setColor(Color.GREEN);
g.drawOval(190, 140, 50, 50);
}
}
3.
import java.applet.Applet;
import java.awt.*;
/*
<applet code="DesktopComputer.class" width="600" height="400">
</applet>
*/
public class DesktopComputer extends Applet {
public void paint(Graphics g) {
setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.drawRect(50, 50, 200, 150);
g.setColor(Color.GRAY);
g.fillRect(51, 51, 198, 148);
g.setColor(Color.BLACK);
g.drawRect(120, 200, 60, 20);
g.fillRect(140, 220, 20, 30);
g.setColor(Color.BLACK);
g.drawRect(300, 100, 100, 200);
g.setColor(Color.GRAY);
g.fillRect(301, 101, 98, 198);
g.setColor(Color.BLACK);
g.fillRect(320, 120, 60, 30);
g.fillRect(320, 180, 60, 20);
g.fillRect(320, 230, 60, 50);
g.setColor(Color.BLACK);
g.drawRect(50, 250, 200, 55);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(51, 251, 198, 53);
g.setColor(Color.BLACK);
g.drawOval(260, 280, 40, 60);
g.setColor(Color.GRAY);
g.fillOval(261, 281, 38, 58);
g.setColor(Color.BLACK);
g.drawLine(280, 285, 280, 310);
g.setColor(Color.RED);
g.fillOval(350, 250, 20, 20);
g.setColor(Color.BLACK);
g.drawOval(350, 250, 20, 20);
g.setColor(Color.BLACK);
g.fillRect(310, 150, 10, 5);
g.fillRect(310, 160, 10, 5);
g.fillRect(310, 170, 10, 5);
g.setColor(Color.BLACK);
for (int i = 0; i < 10; i++) {
g.drawRect(60 + i * 18, 260, 15, 15);
g.drawRect(60 + i * 18, 280, 15, 15);
}
}
}
OUTPUT:
1.
2.
3.
DATE:15/05/2024
PROGRAM NO:21
AIM: Display your name and perform addition and multiplication of two numbers
by passing parameters in the applet window.
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<applet code="Name.java" width=500 height=300>
<param name=num1 value=20>
<param name=num2 value=57>
</applet>
*/
public class Name extends Applet {
String name;
int num1, num2;
OUTPUT:
DATE:15/05/2024
PROGRAM NO:22
PROGRAM:
import java.awt.*;
import java.applet.*;
/*<applet code="ImageDisplay.class" width="400" height="300">
</applet>*/
PROGRAM NO:23
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<applet code="simpleBanner" width=300 height=50>
</applet>
*/
public class simpleBanner extends Applet implements Runnable
{
String msg="A simple moving Banner.";
Thread t=null;
int state;
boolean stopFlag;
}
}
}
OUTPUT:
DATE:17/05/2024
PROGRAM NO:24
AIM: a) Implement all the mouse events and key events in an applet window.
b) Draw the structure of a house, Doors and windows are to be represented using
scrollbars. When the scrollbars are moved, the color of the house is to be changed.
PROGRAM:
a)
b) import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<applet code="Event.class" width="400" height="300">
</applet> */
{
Scrollbar redScrollbar, greenScrollbar, blueScrollbar;
Color clr;
{
redScrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
greenScrollbar = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 256);
blueScrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
redScrollbar.addAdjustmentListener(this);
greenScrollbar.addAdjustmentListener(this);
blueScrollbar.addAdjustmentListener(this);
add(redScrollbar);
add(greenScrollbar);
add(blueScrollbar);
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(clr);
g.fillRect(75, 150, 150, 150);
g.setColor(Color.BLACK);
g.drawRect(75, 150, 150, 150);
g.setColor(Color.RED);
int[] xPoints = {75, 150, 225};
int[] yPoints = {150, 75, 150};
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.DARK_GRAY);
g.fillRect(140, 230, 40, 70);
g.setColor(Color.CYAN);
g.fillRect(90, 180, 30, 30);
g.fillRect(180, 180, 30, 30);
g.setColor(Color.BLACK);
g.drawLine(105, 180, 105, 210);
g.drawLine(90, 195, 120, 195);
g.drawLine(195, 180, 195, 210);
g.drawLine(180, 195, 210, 195);
}
}
OUTPUT:
a)
b)
DATE:17/05/2024
PROGRAM NO:25
AIM: Prepare a job resume with required fields.When you click on the submit button , the
summary of entered values are to be displayed in a text area.
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public JobResumeForm() {
setTitle("Job Resume Form");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(5, 2));
add(nameLabel);
add(nameField);
add(emailLabel);
add(emailField);
add(phoneLabel);
add(phoneField);
add(experienceLabel);
add(scrollPane);
add(new JLabel()); add(submitButton);
submitButton.addActionListener(this);
}
nameField.setText("");
emailField.setText("");
phoneField.setText("");
experienceArea.setText("");
}
}
PROGRAM NO:26
AIM: Develop a swing program that displays a story (minimum 5 sentences) in different
fonts using labels.
PROGRAM:
import javax.swing.*;
import java.awt.*;
public class StoryDisplay extends JFrame {
public StoryDisplay() {
setTitle("Story Display");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(5, 1));
String[] sentences = {
"Beneath the crystal waves of the Enchanted Sea, a mermaid named Lyra discovered a
sunken city.",
" Guided by a map etched in pearls, she ventured through the coral-covered ruins.",
"There, she found an ancient trident that pulsed with untold power.",
" As she grasped it, the spirit of an old king appeared, pleading for her to restore his
kingdom.",
" With the trident's magic, Lyra revived the city, bringing life and light back to the
depths."
};
Font[] fonts = {
new Font("Serif", Font.BOLD, 18),
new Font("SansSerif", Font.ITALIC, 20),
new Font("Monospaced", Font.PLAIN, 16),
new Font("Dialog", Font.BOLD + Font.ITALIC, 22),
new Font("Arial", Font.PLAIN, 18)
};
for (int i = 0; i < 5; i++) {
JLabel label = new JLabel(sentences[i]);
label.setFont(fonts[i]);
add(label);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StoryDisplay().setVisible(true);
}
});
}
}
OUTPUT:
DATE:19/05/2024
PROGRAM NO:27
AIM: Develop a swing program with two text fields for accepting username and password.
When you click a submit button, a dialog box is to be displayed with a Welcome message by
specifying the username and password.
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public LoginDialog() {
setTitle("Login");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2));
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(new JLabel()); // Empty cell for spacing
add(submitButton);
submitButton.addActionListener(this);
}
if (username.isEmpty() || passwordString.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter both username and password.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
usernameField.setText("");
passwordField.setText("");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LoginDialog().setVisible(true);
}
});
}
}
OUTPUT:
DATE:20/05/2024
PROGRAM NO:28
AIM: Using Swing, display 3 faces namely joyful, sad and angry faces while clicking 3
different named buttons
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
panel.add(joyButton);
panel.add(sadButton);
panel.add(angryButton);
add(panel);
setTitle("Face Button");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
OUTPUT:
DATE:21/05/2024
PROGRAM NO:29
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = clrButton;
buttonPanel.add(numberButtons[1]);
buttonPanel.add(numberButtons[2]);
buttonPanel.add(numberButtons[3]);
buttonPanel.add(addButton);
buttonPanel.add(numberButtons[4]);
buttonPanel.add(numberButtons[5]);
buttonPanel.add(numberButtons[6]);
buttonPanel.add(subButton);
buttonPanel.add(numberButtons[7]);
buttonPanel.add(numberButtons[8]);
buttonPanel.add(numberButtons[9]);
buttonPanel.add(mulButton);
buttonPanel.add(decButton);
buttonPanel.add(numberButtons[0]);
buttonPanel.add(equButton);
buttonPanel.add(divButton);
buttonPanel.add(clrButton);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clrButton) {
textField.setText("");
textField.repaint();
}
}
}
OUTPUT:
DATE:22/05/2024
PROGRAM NO:30
AIM: Develop a client server application using a) TCP and b) UDP where the server
performs some mathematical operation on a number received from the client side
PROGRAM:
a) TCP
import java.io.*;
import java.net.*;
out.println(result);
System.out.println("Result sent to client: " + result);
in.close();
out.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
out.println(numberStr);
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
b) UDP
import java.io.*;
import java.net.*;
serverSocket.receive(receivePacket);
String numberStr = new String(receivePacket.getData(), 0, receivePacket.getLength());
double number = Double.parseDouble(numberStr);
System.out.println("Number received from client: " + number);
serverSocket.send(sendPacket);
System.out.println("Result sent to client: " + result);
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
try {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
clientSocket.send(sendPacket);
System.out.println("Number sent to server: " + numberStr);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT: