0% found this document useful (0 votes)
83 views

Java Lab Answer 5

The document contains a student's lab assignment responses for questions about serialization and deserialization of objects in Java, creating and manipulating a TreeMap, building a basic calculator application using JavaFX, and displaying an image using JavaFX. The student provides code samples to demonstrate serialization/deserialization of employee data, adding and removing elements from a TreeMap, designing a simple calculator GUI, and loading an image into an ImageView.

Uploaded by

adithkumar menon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Java Lab Answer 5

The document contains a student's lab assignment responses for questions about serialization and deserialization of objects in Java, creating and manipulating a TreeMap, building a basic calculator application using JavaFX, and displaying an image using JavaFX. The student provides code samples to demonstrate serialization/deserialization of employee data, adding and removing elements from a TreeMap, designing a simple calculator GUI, and loading an image into an ImageView.

Uploaded by

adithkumar menon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB ASSIGNMENT-5

NAME:ADITH KUMAR MENON


REGISTRATION NUMBER:18BCE2311

Q1. Writea program to demonstrate the object serialization and


deserialization when one object holds information about the employee of
an organization

ANS:

import java.io.*;

class Employee implements Serializable {


private static final long serialversionUID =129348938L;
transient int salary;
static int dept_id;
String name;
int age;

// Default constructor
public Employee(String name, int age, int salary, int dept_id)
{
this.name = name;
this.age = age;
this.salary = salary;
this.dept_id = dept_id;
}

public class SerialExample {


public static void printdata(Employee object1)
{

System.out.println("name of the employee = " + object1.name);


System.out.println("age of employee = " + object1.age);
System.out.println("salary of employee = " + object1.salary);
System.out.println("department id of employee = " + object1.dept_id);
}

public static void main(String[] args)


{
Employee object = new Employee("adith", 20, 10000, 10);
String filename = "shubham.txt";

// Serialization
try {

// Saving of object in a file


FileOutputStream file = new FileOutputStream
(filename);
ObjectOutputStream out = new ObjectOutputStream
(file);

// Method for serialization of object


out.writeObject(object);

out.close();
file.close();

System.out.println("Object has been serialized\n"


+ "Data before Deserialization.");
printdata(object);

// value of static variable changed


object.dept_id = 2000;
}

catch (IOException ex) {


System.out.println("IOException is caught");
}

object = null;

// Deserialization
try {

// Reading the object from a file


FileInputStream file = new FileInputStream
(filename);
ObjectInputStream in = new ObjectInputStream
(file);
// Method for deserialization of object
object = (Employee)in.readObject();

in.close();
file.close();
System.out.println("Object has been deserialized\n"
+ "Data after Deserialization.");
printdata(object);

// System.out.println("z = " + object1.z);


}

catch (IOException ex) {


System.out.println("IOException is caught");
}

catch (ClassNotFoundException ex) {


System.out.println("ClassNotFoundException" +
" is caught");
}
}
}

OUTPUT:
Q2. Createa Treemap and store the name and regd no of your ten number of
friends, finally delete one of your friend data from the map

ANS:

import java.util.*;
class tree_map {
public static void main(String args[])
{

TreeMap tm1 = new TreeMap();

TreeMap tm2 = new TreeMap();

tm1.put(1, "RAKESH :18BCE2351");


tm1.put(2, "DEVOPRASAD :18BCE2312");
tm1.put(3, "BASIL JACOB :18BCE342");
tm1.put(4, "AADARSH SREEKUMAR :18BCE2316");
tm1.put(5, "SRIDHAR MUGUNDAN :18BCE2313");

tm2.put(6, "MANU :18BMA2314");


tm2.put(7, "ASHWIN :18BMA2315");
tm2.put(8, "ABEL :18BMA2456");
tm2.put(9, "NEVIN :18BME2345");
tm2.put(10, "VISHAL :18BCE4578");

System.out.println(tm1);
tm2.remove(6);
System.out.println(tm2);
}
}

OUTPUT:
FOLLOWING JAVAFX PART IS DONE IN ECLIPSE:

3Q. Show one stage using JAVAFX where your name will be displayed

ANS:

package helloworld;

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;

public class HelloWorld extends Application {


public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

StackPane root = new StackPane();


root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

OUTPUT:
4Q. Write a program to design a calculator by using javaFX

ANS:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Calculator extends Application{

private TextField txtNum = new TextField();


private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button zero = new Button("0");
private Button dot = new Button(".");
private Button AC = new Button("AC");
private Button btnSum = new Button("+");
private Button btnSub = new Button("-");
private Button btnMul = new Button("*");
private Button btnDiv = new Button("/");
private Button btnEq = new Button("=");

public static void main(String[] args) {


Application.launch(args);

}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane myPane = new GridPane();
Scene scene = new Scene(myPane,300,300);
primaryStage.setScene(scene);
primaryStage.show();

myPane.add(txtNum, 4, 0);
myPane.add(seven, 1, 3);
myPane.add(eight, 2, 3);
myPane.add(nine, 3, 3);
myPane.add(btnMul, 4, 3);

myPane.add(four, 1, 4);
myPane.add(five, 2, 4);
myPane.add(six, 3, 4);
myPane.add(btnSub, 4, 4);

myPane.add(one, 1, 5);
myPane.add(two, 2, 5);
myPane.add(three, 3, 5);
myPane.add(btnSum, 4, 5);

myPane.add(zero, 1, 6);
myPane.add(dot, 2, 6);
myPane.add(btnDiv, 3, 6);
myPane.add(btnEq, 4, 6);

myPane.setAlignment(Pos.CENTER);

OUTPUT:
5Q. Writea program to design your mobile screen where the icons available
on your screen will be present and your image will be displayed in the
middle

ANS:

DISPLAYING AN IMAGE USING VFX:

package application;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Controller {

//ImageView is a Node used for painting images loaded with Images

// Image = picture
// ImageView = picture frame

@FXML

ImageView myImageView;
Button myButton;

Image myImage = new Image(getClass().getResourceAsStream("CAR2.jpg"));

public void displayImage() {


myImageView.setImage(myImage);
}
}

OUTPUT:

You might also like