Professional Documents
Culture Documents
Code:-: Java Io Java Io Objectoutputstream Demo Java Io Serializable
Code:-: Java Io Java Io Objectoutputstream Demo Java Io Serializable
Lab Assignment-5
Name- Shashwat
Code:-
import java.io.*;
import java.io.ObjectOutputStream;
class Demo implements java.io.Serializable
{
public int age;
public String name;
public String rank;
public Demo(int age, String name, String rank)
{
this.age = age;
this.name = name;
this.rank = rank;
}
class da5_1
{
public static void main(String[] args)
{System.out.println("18BCE0049 Shashwat");
Demo object = new Demo(21, "Shashwat","Student");
String filename = "new.text";
try
{
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(object);
out.close();
file.close();
catch(IOException ex)
{
System.out.println("IOException is caught");
}
in.close();
file.close();
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
}
}
Output:-
2. Create a Treemap and store the name and regd no of your ten number of
friends, finally delete one of your friend data from the map.
Code:-
import java.util.*;
class da5_2{
public static void main(String args[])
{
System.out.println("18BCE0049 SHASHWAT");
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(1, "Abhishek : 18BCE0487");
tm.put(2, "Suresh : 18BCE0059");
tm.put(3, "Ramesh : 18BCE0234");
tm.put(4, "Nilesh : 18BCE0987");
tm.put(5, "Daksh : 18BCE0765");
tm.put(6, "Akshansh : 18BCE0067");
tm.put(7, "Ateet : 18BCE0143");
tm.put(8, "Shayan : 18BCE0866");
tm.put(9, "Nishant : 18BCE0645");
tm.put(10, "Amit : 18BCE0435");
System.out.println(tm);
tm.remove(5);
System.out.println(tm);
}
}
Output:-
3. Show one stage using JAVAFX where your name will be displayed
Code:-
package shashwat;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Shashwat");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Shashwat");
}
});
primaryStage.setTitle("Shashwat");
primaryStage.setScene(scene);
primaryStage.show();
}
Output:-
4. Write a program to design a calculator using Java Fx.
Code:-
package shashwat;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
@FXML
private void onOperatorClick(ActionEvent event) {
if(event.getSource() instanceof Button) {
Button btn = (Button)event.getSource();
if (!outputTF.getText().isEmpty()) {
temp = Double.valueOf(outputTF.getText());
if (btn.getText().equals("%")) {
temp = sum * temp / 100;
}
switch (operatorPressed) {
case "/":
sum /= temp;
break;
case "X":
sum *= temp;
break;
case "+":
sum += temp;
break;
case "-":
sum -= temp;
break;
default:
sum = temp;
}
}
if (btn.getText().equals("=") || btn.getText().equals("%")) {
outputTF.setText(String.valueOf(sum));
operatorPressed = "";
} else {
outputTF.setText("");
operatorPressed = btn.getText().trim();
}
isOperatorPressed = true;
}
}
@FXML
private void onDELClick(ActionEvent event) {
if(outputTF.getText().length() > 0) {
outputTF.setText(outputTF.getText(0, outputTF.getText().length() - 1));
}
}
@FXML
private void onCEClick(ActionEvent event) {
outputTF.setText("");
temp = 0.0;
sum = 0.0;
isOperatorPressed = false;
operatorPressed = "";
}
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("CalculatorUI.fxml"));
stage.setScene(scene);
stage.show();
}
Code:-
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.text.Font;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import java.io.IOException;
text.setFont(new Font(45));
text.setX(100);
text.setY(1000);
text.setFill(Color.RED);
gridPane.setVgap(60);
gridPane.setHgap(60);
gridPane.setAlignment(Pos.CENTER);
gridPane.add(button1, 0, 0, 1, 1);
gridPane.add(button2, 1, 0, 1, 1);
gridPane.add(button3, 2, 0, 1, 1);
gridPane.add(button4, 0, 1, 1, 1);
Output:-