Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Java Programming (CSE1007)

Lab Assignment-5

Name- Shashwat

Reg No- 18BCE0049

Slot- L53 + L54

Faculty- Prof. Asis Kumar Tripathy

1. Write a program to demonstrate the object serialization and deserialization


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

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();

System.out.println("Object has been serialized");

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

Demo object1 = null;


try
{
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
object1 = (Demo)in.readObject();

in.close();
file.close();

System.out.println("Object has been deserialized ");


System.out.println("employee: ");
System.out.println("name = " + object1.name);
System.out.println("age = " + object1.age);
System.out.println("rank = " + object1.rank);
}

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;

public class Shashwat extends Application {

@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");
}
});

StackPane root = new StackPane();


root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Shashwat");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {


launch(args);
}

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;

public class CalculatorUIController implements Initializable {

Double temp = 0.0, sum = 0.0;


boolean isOperatorPressed;
String operatorPressed = "";
TextField outputTF;
@Override
public void initialize(URL url, ResourceBundle rb) {
outputTF.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValu
e) {
if (!newValue.matches("\\d*([\\.]\\d*)?")) {
outputTF.setText(oldValue);
}
}
});
}
@FXML
private void onNumberClick(ActionEvent event) {
if(event.getSource() instanceof Button) {
Button btn = (Button)event.getSource();
if(isOperatorPressed) {
outputTF.setText(btn.getText().trim());
} else {
outputTF.setText(outputTF.getText().trim() + btn.getText().trim());
}
isOperatorPressed = false;
}
}

@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 = "";
}
}

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("CalculatorUI.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {


launch(args);
}
}
Output:-
5. Write a 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.

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;

public class Gridques extends Application {


@Override
public void start(Stage stage) throws IOException {

Text text = new Text();

text.setFont(new Font(45));

text.setX(100);
text.setY(1000);
text.setFill(Color.RED);

text.setText("Shashwat’s mobile phone");


text.setFont(Font.font("TIMES NEW ROMAN",FontWeight.BOLD,FontPosture.REGULAR,25));

InputStream stream = new FileInputStream("C:\\Users\\Users\\Desktop\\shashwat.PNG");


Image image = new Image(stream);
//Creating the image view
ImageView imageView = new ImageView();
//Setting image to the image view
imageView.setImage(image);
//Setting the image view parameters
imageView.setX(10);
imageView.setY(50);
imageView.setFitWidth(800);
imageView.setPreserveRatio(true);

Button button1 = new Button("Contacts");


Button button2 = new Button("Files");
Button button3 = new Button("Camera");
Button button4 = new Button("SPOTIFY");

GridPane gridPane = new GridPane();


gridPane.setMinSize(800, 400);

gridPane.setPadding(new Insets(80, 80, 80, 80));

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);

Group root = new Group();


root.getChildren().addAll(imageView, text, gridPane);

//Creating a scene object


Scene scene = new Scene(root);

//Setting title to the Stage


stage.setTitle("Shashwat");

//Adding scene to the stage


stage.setScene(scene);

//Displaying the contents of the stage


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

Output:-

You might also like