0% found this document useful (0 votes)
44 views11 pages

Java Programming Lab Assignment - 5

The document contains code for two Java programming assignments. The first assignment involves creating a generic method to count the number of times a string occurs in an array and sorting the array. The second assignment creates a JavaFX application with 5 buttons that print the button color when clicked.

Uploaded by

Ankur
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)
44 views11 pages

Java Programming Lab Assignment - 5

The document contains code for two Java programming assignments. The first assignment involves creating a generic method to count the number of times a string occurs in an array and sorting the array. The second assignment creates a JavaFX application with 5 buttons that print the button color when clicked.

Uploaded by

Ankur
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/ 11

JAVA PROGRAMMING

LAB ASSIGNMENT – 5

NAME – ANKUR KUNDU


REG.NO. – 17BIS0020
COURSE CODE – CSE1007
FACULTY – Prof. JANANI T
SLOT – L1 + L2
Question 1 - Create a generic method to count the
number of times a given string “Rohit” occurs in
an array of strings named
Highscores={“Rohit”,”Rohit”,”Sehwag”,
“Sachin”,”Anwar”} and print the count value.
Also sort the name of players in ascending order
without using an inbuilt function and print the
result

CODE –
package LabTask5_17BIS0020;

import java.util.Arrays;

public class Labtask5_17BIS0020


{
public static <E> void count(E[] arr, E e)
{
int c = 0;
for(E element : arr)
{
if(element.equals(e))
{
c++;
}
}
System.out.println("No. of times the String 'Rohit' repeats
is: " + c);
}
public static <A> void printArray(A[] arr)
{
for(A i : arr)
{
System.out.println(i);
}
}
public static String[] sortStringArray(String[] arr)
{
int size = arr.length;

for(int i = 0; i<size-1; i++)


{
for (int j = i+1; j<arr.length; j++)
{
if(arr[i].compareTo(arr[j])>0)
{
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}

public static void main(String[] args)


{
// TODO code application logic here
String[] Highscores = {"Rohit","Rohit","Sehwag",
"Sachin","Anwar"};
count(Highscores, "Rohit");
System.out.println("\nThe sorted Array of Highscores
is(using Arrays.sort() method): ");
Arrays.sort(Highscores);
printArray(Highscores);
System.out.println("\nThe sorted Array of Highscores
is(using self-defined method): ");
sortStringArray(Highscores);
printArray(Highscores);
}

}
OUTPUT -

Question 2 - Write a JavaFX program to create a


page with 5 buttons named "red", "green", "blue",
"pink", "black", on clicking the button, print the
color name in the console.

CODE –
Package Labtask5_17BIS0020;

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;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
import javafx.scene.paint.*;

public class Labtask5_17BIS0020 extends Application {

@Override
public void start(Stage primaryStage) {
Button btn1 = new Button();
btn1.setText("Red");

Button btn2 = new Button();


btn2.setText("Green");

Button btn3 = new Button();


btn3.setText("Blue");

Button btn4 = new Button();


btn4.setText("Pink");

Button btn5 = new Button();


btn5.setText("Black");

TilePane root = new TilePane();


root.getChildren().add(btn1);
root.getChildren().add(btn2);
root.getChildren().add(btn3);
root.getChildren().add(btn4);
root.getChildren().add(btn5);

Scene scene = new Scene(root, 500, 500);

btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
BackgroundFill background_fill = new
BackgroundFill(Color.RED,
CornerRadii.EMPTY, Insets.EMPTY);

Background background = new


Background(background_fill);

btn1.setBackground(background);
System.out.println("Red");
}
});

btn2.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
BackgroundFill background_fill = new
BackgroundFill(Color.GREEN,
CornerRadii.EMPTY, Insets.EMPTY);

Background background = new


Background(background_fill);

btn2.setBackground(background);
System.out.println("Green");

}
});

btn3.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
BackgroundFill background_fill = new
BackgroundFill(Color.BLUE,
CornerRadii.EMPTY, Insets.EMPTY);

Background background = new


Background(background_fill);

btn3.setBackground(background);
System.out.println("Blue");

}
});

btn4.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
BackgroundFill background_fill = new
BackgroundFill(Color.PINK,
CornerRadii.EMPTY, Insets.EMPTY);

Background background = new


Background(background_fill);

btn4.setBackground(background);
System.out.println("Pink");

}
});

btn5.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
BackgroundFill background_fill = new
BackgroundFill(Color.BLACK,
CornerRadii.EMPTY, Insets.EMPTY);

Background background = new


Background(background_fill);

btn5.setBackground(background);
System.out.println("Black");
}
});

primaryStage.setTitle("Labtask5_17BIS0020");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

OUTPUT –

You might also like