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

A

Laboratory File

For
EC321: Java Programming

Prepared by
Ramnesh Kumar
Tiwari(20EC084)
Semester – 6, Branch – B.Tech. (EC)
Academic Year 2022-23

Under the supervision of


Dr. Hardik P. Modi

Submitted to

Charotar University of Science & Technology


for Partial Fulfillment of the Requirements of the
Degree of Bachelor of Technology in Electronics & Communication

Submitted at
DEPARTMENTOFELECTRONICS&COMMUNICATION
Faculty of Technology & Engineering, CHARUSAT

Chandubhai S. Patel Institute of Technology


At: Changa, Dist: Anand – 388421
April 2023
CERTIFICATE

This is to certify that the laboratory file for EC321: Java Programming is a bonafied work
carried out by under the
guidance and supervision of for
of Bachelor of Technology in Electronics & Communication
at Faculty of Technology & Engineering (C.S.P.I.T.) – CHARUSAT, Gujarat.

To the best of my knowledge and belief, this work embodies the work of candidate
himself/herself, has duly been completed, and fulfills the requirement of the course
and is up to the standard in respect of content, presentation and language for being
referred to the examiner.

Under the supervision of,

Dr. Hardik P. Modi


Associate Professor
Department of EC Engineering,
C.S.P.I.T., CHARUSAT-Changa.

Dr. Upesh P. Patel


Head of Department,
Department of EC Engineering
C.S.P.I.T., CHARUSAT- Changa, Gujarat.

Chandubhai S Patel Institute of Technology (C.S.P.I.T.)


Faculty of Technology & Engineering, CHARUSAT

At: Changa, Ta. Petlad, Dist. Anand, PIN: 388 421. Gujarat
CHARUSAT
CSPIT, FTE
V.T. Patel Department of Electronics and
Communication

SR. No. Name of Practical Signature


1. Write a Java program to add two numbers.
2. Write a Java program to check whether a number is even or
odd.
3. Write a Java program to add two binary numbers.
4. Write a Java program to add two complex numbers.
5. Write a Java program to multiply two numbers.
6. Write a Java program to check leap year.
7. Write a Java program to check whether the input character is a
vowel or consonant.
8. Write a Java program to calculate compound interest.
9. Write a Java program to calculate simple interest.
10. Write a Java program to find quotient and remainder.
11. Write a Java program to calculate the Power of a number.
12. Write a Java program to print a diamond pattern.
13. Write a Java program to convert Char to String.
14. Write a Java program to convert String to Char.
15. Write a Java program to find all the subsets of a string.
16. Write a Java Program to remove all white spaces from a String.
17. Write a Java Program to count Vowels and Consonants from a
String.
18. Write a Java Program to find the number of elements in an
Array.
19. Write a Java Program to add the elements in an Array.
20. Write a Java Program to find the largest element in an Array.
21. Write a Java Program to find the smallest element in an Array.
22. Write a Java Program to find the average of numbers using an
Array.
23. Write a Java Program to find the reverse of a Number.
24. Write a Java Program to reverse a string using recursion.
25. Write a Java Program to find the factorial of a Number using
recursion.
26. Write a Java Program to check whether the input number is
prime or not.
CHARUSAT
CSPIT, FTE
V.T. Patel Department of Electronics and
Communication
27. Write a Java Program to check whether the input number is
perfect square or not.
28. Write a Java Program to find the sum of Natural numbers.
29. Write a Java Program to check whether the number is positive
or negative.
30. Write a Java Program to find the largest of three numbers.
31. Write a Java Program to find the average of three numbers.
32. Write a Java Program to read the number entered by the user.
33. Write a Java Program to get an IP Address.
34. Write a Java Program to get input from user.
35. Write a Java Program to calculate area of rectangle.
36. Write a Java Program to calculate area of square.
37. Write a Java Program to calculate area and circumference of
circle
38. Write a Java Program to calculate area and circumference of
circle
39. Write a Java Program to do sorting in ascending & descending
order using Bubble sort algorithm.
40. Write a Java Program to use linear search algorithm to find out
a number among all other numbers entered by user.
41. Write a Java Program to perform binary search on a list of
integer numbers.
42. Write a Java Program to perform selection sorting.
43. Write a Java Program to implement merge sort.
44. Write a Java Program for octal to decimal conversion.
45. Write a Java Program for decimal to octal conversion.
46. Write a Java Program for binary to decimal conversion.
47. Write a Java Program for decimal to binary conversion.
48. Write a Java Program for character to integer conversion.
49. Write a Java Program to print Fibonacci series.
50. Write a Java Program to find factorial of a number using for
loop.
EC321: Java Programming 20EC084

Program #1
AIM: Write a Java program to add two numbers.

CODE:
import java.util.Scanner;
public class Main {

public static void main(String[] args)

{int num1, num2, sum;


Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number: ");
num1 = sc.nextInt();

System.out.print("Enter Second Number: ");


num2 = sc.nextInt();

sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

OUTPUT:

1
EC321: Java Programming 20EC084

Program #2
AIM: Write a Java program to check whether a number is Even or Odd.

CODE:
import java.util.Scanner;
public class Main {

public static void main(String[] args)

{Scanner scan = new Scanner(System.in);

System.out.print("Enter any number: ");


int num = scan.nextInt();

String evenOrOdd = (num % 2 == 0) ? "even number" : "odd number";

System.out.println(num + " is an " + evenOrOdd);

}
}

OUTPUT:

2
EC321: Java Programming 20EC084

Program #3
AIM: Write a java program to add two binary numbers.

CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
long b1, b2;
int i = 0, carry = 0;

int[] sum = new int[10];

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first binary number: ");


b1 = scanner.nextLong();
System.out.print("Enter second binary number: ");
b2 = scanner.nextLong();

//closing scanner after use to avoid memory leak


scanner.close();
while (b1 != 0 || b2 != 0)
{
sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
b1 = b1 / 10;
b2 = b2 / 10;
}
if (carry != 0)
{ sum[i++] = carry;
}
--i;
System.out.print("Output: ");
while (i >= 0) {
System.out.print(sum[i--]);
}
System.out.print("\n");
}
}

OUTPUT:

3
EC321: Java Programming 20EC084

Program #4
AIM: Write a Java program to add two complex numbers.

CODE:
public class

ComplexNumber{double

real, img;

//constructor to initialize the complex number


ComplexNumber(double r, double i){
this.real = r;
this.img = i;
}

public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)


{

ComplexNumber temp = new ComplexNumber(0, 0);

temp.real = c1.real + c2.real;


temp.img = c1.img + c2.img;

return temp;
}
public static void main(String args[]) {
ComplexNumber c1 = new ComplexNumber(5.5, 4);
ComplexNumber c2 = new ComplexNumber(1.2, 3.5);
ComplexNumber temp = sum(c1, c2);
System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i");
}
}

OUTPUT:

4
EC321: Java Programming 20EC084

Program #5
AIM: Write a Java Program to multiply two numbers.

CODE:
import java.util.Scanner;

public class Demo {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter first number: ");

double num1 = scan.nextDouble();

System.out.print("Enter second number: ");


double num2 = scan.nextDouble();
scan.close();

double product = num1*num2;

System.out.println("Output: "+product);
}
}

OUTPUT:

5
EC321: Java Programming 20EC084

Program #6
AIM: Write a java program to check leap year.

CODE:
import java.util.Scanner;
public class Demo {

public static void main(String[] args) {

int year;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;

if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}

else {
isLeap = false;
}

if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}

OUTPUT:

6
EC321: Java Programming 20EC084

Program #7
AIM: Write a Java program to check whether the input character is a vowel or consonant.

CODE:
import java.util.Scanner;
class JavaExample
{
public static void main(String[ ] arg)
{
boolean isVowel=false;;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a character : ");
char ch=scanner.next().charAt(0);
scanner.close();
switch(ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : isVowel = true;
}
if(isVowel == true)
{ System.out.println(ch+" is a
Vowel");
}
else {
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println(ch+" is a Consonant");
else
System.out.println("Input is not an alphabet");

}
}
}

OUTPUT:

7
EC321: Java Programming 20EC084

Program #8
AIM: Write a Java Program to Calculate Compound Interest.

CODE:
public class Ci {

public void calculate(int p, int t, double r, int n)


{double amount = p * Math.pow(1 + (r / n), n * t);
double cinterest = amount - p;
System.out.println("Compound Interest after " + t + " years:
"+cinterest);
System.out.println("Amount after " + t + " years: "+amount);
}
public static void main(String args[]) {
Ci obj = new Ci();
obj.calculate(2000, 5, .08, 12);
}
}

OUTPUT:

8
EC321: Java Programming 20EC084

Program #9
AIM: Write a Java Program to Calculate Simple Interest.

CODE:
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
float p, r, t, sinterest;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Principal : ");


p = scan.nextFloat();

System.out.print("Enter the Rate of interest : ");


r = scan.nextFloat();

System.out.print("Enter the Time period : ");


t = scan.nextFloat();
scan.close();

sinterest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +sinterest);
}
}

OUTPUT:

9
EC321: Java Programming 20EC084

Program #10
AIM: Write a Java program to find Quotient and Remainder.

CODE:
public class Main {
public static void main(String[] args)
{int num1 = 15, num2 = 7;
int quotient = num1 / num2;
int remainder = num1 % num2;
System.out.println("Quotient is: " + quotient);
System.out.println("Remainder is: " + remainder);
}
}

OUTPUT:

10
EC321: Java Programming 20EC084

Program #11
AIM: Write a Java Program to calculate the Power of a number.

CODE:
public class Main {
public static void main(String[] args)
{int number = 2, p = 5;
long result = 1;

//Copying the exponent value to the loop counter


int i = p;
for (;i != 0; --i)
{
result *= number;
}
System.out.println(number+" power "+p+" is :");
System.out.print(number+"^"+p+" = "+result);
}
}

OUTPUT:

11
EC321: Java Programming 20EC084

Program #12
AIM: Write a java program to print a diamond pattern.

CODE:
public class Main {
public static void main(String[] args)
{
// Initializing the variable with the number at which
// the number of stars will be maximum. In this case, the
// stars will increase till 7th row, 7th row will have max
// stars and the stars will start decreasing after 7th row.
int numberOfRows = 6;

int i, j;

//This loop will print the first half of the diamond pattern
for (i = 1; i <= numberOfRows; i++) {

// This will print whitespaces, 6 spaces in first row


// 5 spaces in second row and so on
for (j = 1; j <= numberOfRows - i; j++)
{System.out.print(" ");
}

// This will print the stars after the whitespaces printed


// by above loop. It will print 1 star in first row, 3 in second
// 5 in third and so on
for (j = 1; j <= i * 2 - 1; j++)
{System.out.print("*");
}

// Move the cursor to new line after each row


System.out.println();
}

//This loop will print the second half of the diamond pattern
for (i = numberOfRows - 1; i > 0; i--) {

// This will print whitespaces in second half of triangle pattern


for (j = 1; j <= numberOfRows - i; j++) {
System.out.print(" ");
}

// This will print stars in second half of triangle pattern


for (j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}

12
EC321: Java Programming 20EC084

// Move the cursor to new line after each row


System.out.println();
}
}
}

OUTPUT:

13
EC321: Java Programming 20EC084

Program #14
AIM: Write a Java Program to convert Char to String.

CODE:
class CharToString
{
public static void main(String args[])
{
char ch = 'a';
String str = Character.toString(ch);
System.out.println("String is: "+str);
}
}

OUTPUT:

14
EC321: Java Programming 20EC084

Program #15
AIM: Write a Java Program to convert String to Char.

CODE:
class StringToCharDemo
{
public static void main(String args[])
{
// Using charAt() method
String str = "CHARUSAT";
for(int i=0;
i<str.length();i++){char ch =
str.charAt(i);
System.out.println("Character at "+i+" Position: "+ch);
}
}
}

OUTPUT:

15
EC321: Java Programming 20EC084

Program #16
AIM: Write a Java Program to find all the subsets of a String.

CODE:
public class JavaExample {
public static void main(String[] args) {

String str = "Java";


//finding length of the given string
int numberOfChar = str.length();
int temp = 0;
//If number of characters in a string is n then the
//possible subsets of that string is n*(n+1)/2
String subsetArray[] = new
String[numberOfChar*(numberOfChar+1)/2];

for(int i = 0; i < numberOfChar; i++)


{ for(int j = i; j < numberOfChar; j++)
{
subsetArray[temp] = str.substring(i, j+1);
temp++;
}
}

//Print all the subsets of the given string


System.out.println("All the possible subsets of the given
string: ");
for(int i = 0; i < subsetArray.length; i++)
{System.out.println(subsetArray[i]);
}
}
}

OUTPUT:

16
EC321: Java Programming 20EC084

Program #17
AIM: Write a Java Program to remove all white spaces from a String.

CODE:
public class WhiteSpaces {
public static void main(String[] args) {

String str="Heyy, I am C2.";


System.out.println("Original String: " + str);

//Using regex inside replaceAll() method to replace space with


blank
str = str.replaceAll("\\s+", "");

System.out.println("String after white spaces are removed: " +str);


}
}

OUTPUT:

17
EC321: Java Programming 20EC084

Program #18
AIM: Write a Java Program to count Vowels and Consonants from a String.

CODE:
public class VowCons {

public static void main(String[] args)


{ String str = "Dev, Parin,
Shitanshu";intvcount = 0, ccount = 0;

//converting all the chars to lowercase


str = str.toLowerCase();
for(int i = 0; i < str.length(); i++) { char ch =
str.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u') { vcount++; } else if((ch >= 'a'&& ch <= 'z')) {ccount++;
}
}
System.out.println("Number of Vowels: " + vcount);
System.out.println("Number of Consonants: " + ccount);
}
}

OUTPUT:

18
EC321: Java Programming 20EC084

Program #19
AIM: Write a Java Program to find the number of elements in an Array.

CODE:
public class NumofElements {
public static void main(String[] args) {
//Initializing an int array
int [] numbers = new int [] {2, 4, 6, 8, 10, 12};
// We can use length property of array to find the count of
elements
System.out.println("Number of elements in the given int array:
" + numbers.length);

//Initializing a String array


String [] names = new String [] {"Dev", "Kartik",
"Shitanshu", "Abhi"};
System.out.println("Number of elements in the given String
array: " + names.length);
}
}

OUTPUT:

19
EC321: Java Programming 20EC084

Program #20
AIM: Write a Java Program to add the elements in an Array.

CODE:
class SumOfArray{
public static void main(String
args[]){int[] array = {65, 81, 85, 86};
int sum = 0;
//Advanced for loop
for( int num : array) {
sum += num;
}
System.out.println("Sum of array elements is: "+sum);
}
}

OUTPUT:

20
EC321: Java Programming 20EC084

Program #21
AIM: Write a Java Program to find the largest element in an Array.

CODE:
public class LargestElement {
public static void main(String[] args) {

//Initializing an int array


int [] arr = new int [] {65, 81, 85, 86};
//This element will store the largest element of the array
//Initializing with the first element of the array
int largestElement = arr[0];
//Running the loop from 1st element till last element
for (int i = 0; i < arr.length; i++) {
//Compare each elements of array with largestElement
//If an element is greater, store the element into
largestElement
if(arr[i] > largestElement)
largestElement = arr[i];
}
System.out.println("Largest element of given array: " +
largestElement);
}
}

OUTPUT:

21
EC321: Java Programming 20EC084

Program #22
AIM: Write a Java Program to find the smallest element in an Array.

CODE:
public class SmallestElement {
public static void main(String[] args) {

//Initializing an int array


int [] arr = new int [] {65, 81, 85, 86};
//This element will store the smallest element of the array
//Initializing with the first element of the array
int smallestElement = arr[0];
//Running the loop from first element till last element
for (int i = 0; i < arr.length; i++) {
//Compare each elements of array with smallestElement
//If an element is smaller, store the element into
smallestElement
if(arr[i] < smallestElement)
smallestElement = arr[i];
}
System.out.println("Smallest element of given array: " +
smallestElement);
}
}

OUTPUT:

22
EC321: Java Programming 20EC084

Program #23
AIM: Write a Java Program to find the average of numbers using an Array.

CODE:
public class Average {

public static void main(String[] args)


{double[] arr = {65, 81, 85, 86};
double total = 0;

for(int i=0; i<arr.length;


i++){total = total +
arr[i];
}

/* arr.length returns the number of elements


* present in the array
*/
double average = total / arr.length;

System.out.format("The average is: %.3f", average);


}
}

OUTPUT:

23
EC321: Java Programming 20EC084

Program #24
AIM: Write a Java Program to find the reverse of a Number.

CODE:
import java.util.Scanner;
class Reverse
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");

Scanner in = new Scanner(System.in);

num = in.nextInt();
/* for loop: No initialization part as num is already
* initialized and no increment/decrement part as logic
* num = num/10 already decrements the value of num
*/
for( ;num != 0; )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}

System.out.println("Reverse of specified number is: "+reversenum);


}
}

OUTPUT:

24
EC321: Java Programming 20EC084

Program #25
AIM: Write a Java Program to reverse a string using recursion.

CODE:
public class ReverseStr {

public static void main(String[] args)


{String str = "Attack on Titan";
String reversed = reverseString(str);
System.out.println("The reversed string is: " + reversed);
}

public static String reverseString(String str)


{
if (str.isEmpty())
return str;
//Calling Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}

OUTPUT:

25
EC321: Java Programming 20EC084

Program #26
AIM: Write a Java Program to find the factorial of a Number using recursion.

CODE:
import java.util.Scanner;
class Factorial{
public static void main(String args[]){

Scanner scanner = new Scanner(System.in);


System.out.println("Enter the number: ");

int num = scanner.nextInt();


int factorial = fact(num);
System.out.println("Factorial of entered number is:
"+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}

OUTPUT:

26
EC321: Java Programming 20EC084

Program #27
AIM: Write a Java Program to check whether the input number is prime or not.

CODE:
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number: ");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}

if(isPrime)
System.out.println(num + " is a Prime Number.");
else
System.out.println(num + " isn’t a Prime Number.");
}
}

OUTPUT:

27
EC321: Java Programming 20EC084

Program #28
AIM: Write a Java Program to check whether the input number is perfect square or not.

CODE:
import java.util.Scanner;
class PerfectSquare {

static boolean checkPerfectSquare(double x)


{
// finding the square root of given number
double sq = Math.sqrt(x);
return ((sq - Math.floor(sq)) == 0);
}

public static void main(String[] args)


{
System.out.print("Enter any number: ");
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();
scanner.close();

if (checkPerfectSquare(num))
System.out.print(num+ " is a perfect square number.");
else
System.out.print(num+ " is not a perfect square number.");
}
}

OUTPUT:

28
EC321: Java Programming 20EC084

Program #29
AIM: Write a Java Program to find the sum of Natural numbers.

CODE:
public class SumofNatural {

public static void main(String[] args) {

int num = 86, count, total = 0;

for(count = 1; count <= num; count++){


total = total + count;
}

System.out.println("Sum of first 10 natural numbers is: "+total);


}
}

OUTPUT:

29
EC321: Java Programming 20EC084

Program #30
AIM: Write a Java Program to check whether the number is positive or negative.

CODE:
public class Main
{
public static void main(String[] args)
{
int number=109;
if(number > 0)
{
System.out.println(number+" is a positive number");
}
else if(number < 0)
{
System.out.println(number+" is a negative number");
}
else
{
System.out.println(number+" is neither positive nor negative");
}
}
}

OUTPUT:

30
EC321: Java Programming 20EC084

Program #31
AIM: Write a Java Program to find the largest of three numbers.

CODE:
public class Largestof3{

public static void main(String[] args) {

int num1 = 65, num2 = 81, num3 = 86;

if( num1 >= num2 && num1 >= num3)


System.out.println(num1+" is the largest Numbe.r");

else if (num2 >= num1 && num2 >= num3)


System.out.println(num2+" is the largest Number.");

else
System.out.println(num3+" is the largest Number.");
}
}

OUTPUT:

31
EC321: Java Programming 20EC084

Program #32
AIM: Write a Java Program to find the average of three numbers.

CODE:
import java.util.Scanner;
public class Average {

public static void main(String[] args)


{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scan.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scan.nextDouble();
System.out.print("Enter the third number: ");
double num3 = scan.nextDouble();
scan.close();
System.out.print("The average of entered numbers is: " +
avr(num1, num2, num3) );
}

public static double avr(double a, double b, double c)


{
return (a + b + c) / 3;
}
}

OUTPUT:

32
EC321: Java Programming 20EC084

Program #33
AIM: Write a Java Program to read the number entered by the user.

CODE:
import java.util.Scanner;
public class ReadNum {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter any number: ");

// This method reads the number provided using keyboard


int num = scan.nextInt();
scan.close();

System.out.println("The number entered by user: "+num);


}
}

OUTPUT:

33
EC321: Java Programming 20EC084

Program #34
AIM: Write a Java Program to get an IP Address.

CODE:
import java.net.InetAddress;

class GetMyIPAddress
{
public static void main(String args[]) throws Exception
{
InetAddress myIP=InetAddress.getLocalHost();

System.out.println("My IP Address is: ");


System.out.println(myIP.getHostAddress());
}
}

OUTPUT:

34
EC321: Java Programming 20EC084

Program #35
AIM: Write a Java Program to get input from user

CODE:
import java.util.Scanner;

class Main
{
public static void main(String args[])
{
int num;
float fnum;
String str;

Scanner in = new Scanner(System.in);

System.out.print("Enter a string: ");


str = in.nextLine();
System.out.println("Input String is: "+str);

System.out.print("Enter an integer: ");


num = in.nextInt();
System.out.println("Input Integer is: "+num);

System.out.print("Enter a float number: ");


fnum = in.nextFloat();
System.out.println("Input Float number is: "+fnum);
}
}

OUTPUT:

35
EC321: Java Programming

Program #36
AIM: Write a Java Program to calculate area of rectangle.

CODE:
import java.util.Scanner;
class Main {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of
Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of
Rectangle:");
double width = scanner.nextDouble();
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}

OUTPUT:

36
EC321: Java Programming 20EC084

Program #37
AIM: Write a Java Program to calculate area of square.

CODE:
import java.util.Scanner;
class Main {
public static void main (String[] args)
{
System.out.println("Enter Side of Square:");
Scanner scanner = new Scanner(System.in);
double side = scanner.nextDouble();
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}

OUTPUT:

37
EC321: Java Programming 20EC084

Program #38
AIM: Write a Java Program to calculate area of triangle.

CODE:
import java.util.Scanner;
class AreaTriangle {
public static void main(String args[])
{ Scanner scanner = new
Scanner(System.in);

System.out.println("Enter the width of the Triangle:");


double base = scanner.nextDouble();

System.out.println("Enter the height of the Triangle:");


double height = scanner.nextDouble();

//Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}

OUTPUT:

38
EC321: Java Programming 20EC084

Program #39
AIM: Write a Java Program to calculate area and circumference of circle.

CODE:
class Main
{
public static void main(String args[])
{
int radius = 3;

double area = Math.PI * (radius * radius);


System.out.printf("Area is: %.2f", area);

double circumference= Math.PI * 2*radius;


System.out.printf( "\nCircumference is: %.2f",circumference) ;
}
}

OUTPUT:

39
EC321: Java Programming 20EC084

Program #39
AIM: Write a Java Program to do sorting in ascending & descending order using Bubble
sort algorithm.

CODE:
import java.util.Scanner;

class BubbleSortExample {
public static void main(String []args)
{int num, i, j, temp;
Scanner input = new Scanner(System.in);

System.out.println("Enter the number of integers to sort:");


num = input.nextInt();

int array[] = new int[num];

System.out.println("Enter " + num + " integers: ");

for (i = 0; i < num; i++)


array[i] = input.nextInt();

for (i = 0; i < ( num - 1 ); i++) {


for (j = 0; j < num - i - 1; j++)
{if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

System.out.println("Sorted list of integers:");

for (i = 0; i < num; i++)


System.out.println(array[i]);
}
}

40
EC321: Java Programming 20EC084

OUTPUT:

41
EC321: Java Programming 20EC084

Program #40
AIM: Write a Java Program to use linear search algorithm to find out a number among all
other numbers entered by user.

CODE:
import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int counter, num, item, array[];

Scanner input = new Scanner(System.in);


System.out.println("Enter number of elements:");
num = input.nextInt();

array = new int[num];


System.out.println("Enter " + num + " integers");

for (counter = 0; counter < num; counter++)


array[counter] = input.nextInt();

System.out.println("Enter the search value:");


item = input.nextInt();

for (counter = 0; counter < num; counter++)


{
if (array[counter] == item)
{
System.out.println(item+" is present at location
"+(counter+1));
break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}

42
EC321: Java Programming 20EC084

OUTPUT:

43
EC321: Java Programming 20EC084

Program #41
AIM: Write a Java Program to perform binary search on a list of integer numbers.

CODE:
import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();

array = new int[num];

System.out.println("Enter " + num + " integers");


for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();

System.out.println("Enter the search value:");


item = input.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + " found at location " + (middle + 1)
+ ".");
break;
}
else
{
last = middle - 1;
}

middle = (first + last)/2;


}
if ( first > last )
System.out.println(item + " is not found.\n");
}
44
EC321: Java Programming 20EC084

45
EC321: Java Programming 20EC084

OUTPUT:

46
EC321: Java Programming 20EC084

Program #42
AIM: Write a Java Program to perform selection sorting.

CODE:
class JavaExample
{
void selectionSort(int arr[])
{
int len = arr.length;

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


{
// Finding the minimum element in the unsorted part of array
int min = i;
for (int j = i+1; j < len; j++)
if (arr[j] < arr[min])
min = j;

int temp = arr[min];


arr[min] = arr[i];
arr[i] = temp;
}
}

// Displays the array elements


void printArr(int arr[])
{
for (int i=0; i<arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}

public static void main(String args[])


{
JavaExample obj = new JavaExample();
int numarr[] = {65,45,18,11,81, 86};

System.out.println("Original array: ");


obj.printArr(numarr);

//calling method for selection sorting


obj.selectionSort(numarr);

System.out.println("Sorted array: ");


obj.printArr(numarr);
}
}
47
EC321: Java Programming 20EC084

OUTPUT:

48
EC321: Java Programming 20EC084

Program #43
AIM: Write a Java Program to implement merge sort.

CODE:
import java.util.Arrays;
class JavaExample {

// Merge two sub arrays L and R into array


void merge(int array[], int lSide, int mid, int rSide) {

//setting size of left and right array


int len1 = mid - lSide + 1;
int len2 = rSide - mid;

int L[] = new int[len1];


int R[] = new int[len2];

// filling the L and R arrays


for (int i = 0; i < len1; i++)
L[i] = array[lSide + i];
for (int j = 0; j < len2; j++)
R[j] = array[mid + 1 + j];

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;
j = 0;
k = lSide;

// Until we reach the end of either left or right array,


compare
// corresponding elements of L and R array and place the
// smaller element in the array to maintain the ascendingorder.
// for sorting in descending order use if(L[i] >= <[j])
while (i < len1 && j < len2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;

49
EC321: Java Programming 20EC084

// Place the remaining elements in the array


while (i < len1) {
array[k] = L[i];
i++;
k++;
}

while (j < len2)


{array[k] = R[j];
j++;
k++;
}
}

// Divide the array into two sub arrays, sort them using
recursion and
// merge them by calling the method merge() that we have created
above
void mergeSort(int array[], int firstIndex, int lastIndex)
{if (firstIndex < lastIndex) {

// m is mid point that is the division point


int mid = (firstIndex + lastIndex) / 2;

// recursive call to this method by passing the sub arrays


mergeSort(array, firstIndex, mid);
mergeSort(array, mid + 1, lastIndex);

// Merge the sorted sub arrays by calling merge() method


merge(array, firstIndex, mid, lastIndex);
}
}

public static void main(String args[]) {

// unsorted array
int[] array = { 81, 65, 7, 20, 18, 86 };
System.out.println("Unsorted Array: ");
System.out.println(Arrays.toString(array));

JavaExample obj = new JavaExample();

// calling the method mergeSort() to sort the array


// firstIndex is 0 (first element), lastIndex is size-1

50
EC321: Java Programming 20EC084

obj.mergeSort(array, 0, array.length - 1);

System.out.println("Sorted Array: ");


System.out.println(Arrays.toString(array));
}
}

OUTPUT:

51
EC321: Java Programming 20EC084

Program #52
AIM: Write a Java Program for octal to decimal conversion.

CODE:
public class JavaExample{
public static void main(String args[]) {
//octal value
String onum = "157";

//octal to decimal using Integer.parseInt()


int num = Integer.parseInt(onum, 8);

System.out.println("Decimal equivalent of Octal value 157


is: "+num);
}
}

OUTPUT:

52
EC321: Java Programming 20EC084

Program #53
AIM: Write a Java Program for decimal to octal conversion.

CODE:
public class JavaExample{
public static void main(String
args[]){int decimalNumber = 80;
String octalNumber = Integer.toOctalString(decimalNumber);
System.out.println("Decimal to Octal: "+octalNumber);
}
}

OUTPUT:

53
EC321: Java Programming 20EC084

Program #54
AIM: Write a Java Program for binary to decimal conversion.

CODE:
import java.util.Scanner;
class Main {
public static void main (String[] args)
{
System.out.println("Enter Side of Square:");
Scanner scanner = new Scanner(System.in);
double side = scanner.nextDouble();
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}

OUTPUT:

54
EC321: Java Programming

Program #55
AIM: Write a Java Program for decimal to binary conversion.

CODE:
class DecimalBinaryExample{

public static void main(String a[]){


System.out.println("Binary representation of 124: ");
System.out.println(Integer.toBinaryString(124));
System.out.println("\nBinary representation of 45: ");
System.out.println(Integer.toBinaryString(45));
System.out.println("\nBinary representation of 999: ");
System.out.println(Integer.toBinaryString(999));
}
}

OUTPUT:

55
EC321: Java Programming 20EC084

Program #56
AIM: Write a Java Program for character to integer conversion.

CODE:
public class CharToInt{
public static void main(String
args[]){char ch = 'V';
char ch2 = 'S';
int num = ch;
int num2 = ch2;
System.out.println("ASCII value of char "+ch+ " is:
"+num);
System.out.println("ASCII value of char "+ch2+ " is:
"+num2);
}

OUTPUT:

56
EC321: Java Programming 20EC084

Program #56
AIM: Write a Java Program to print Fibonacci series.

CODE:
public class Fibonacci {

public static void main(String[] args)

{int count = 11, num1 = 0, num2 = 1;


System.out.print("Fibonacci Series of "+count+" numbers:");

for (int i = 1; i <= count; ++i)


{
System.out.print(num1+" ");

int sumOfPrevTwo = num1 + num2;


num1 = num2;
num2 = sumOfPrevTwo;
}
}
}

OUTPUT:

57
EC321: Java Programming 20EC084

AIM: Write a Java Program to find factorial of a number using for loop.

CODE:
public class JavaExample {

public static void main(String[] args)

{int number = 7;
long fact = 1;
for(int i = 1; i <= number; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

OUTPUT:

58
EC321: Java Programming 20EC084

JAVA PROJECT
EC321: Java Programming 20EC084

PROJECT- QUIZ APLLICTION USING JAVA

package quiz.application;//name of package

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener{

JButton rules,back; //globally defined


JTextField tfname;

Login(){

getContentPane().setBackground(Color.WHITE);
setLayout(null);//It helps to set layout by your own

ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/login.jpeg"));


JLabel image = new JLabel(i1);
image.setBounds(0, 0, 600, 500);
add(image);

JLabel heading = new JLabel("Simple Minds");


heading.setBounds(750, 60, 300,45);
heading.setFont(new Font("Viner Hand ITC", Font.BOLD,40));
heading.setForeground(new Color(30,144,254));//For color of heading
add(heading);

JLabel name = new JLabel("Enter Your name");


name.setBounds(810,150, 300, 20);
name.setFont(new Font("Mongolian Baiti", Font.BOLD,18));
name.setForeground(new Color(30,144,254));//For color of heading
add(name);

//For creating Box to write text


tfname = new JTextField();
tfname.setBounds(735,200,300,25);
tfname.setFont(new Font("Times New Roman", Font.BOLD,20));
add(tfname);

//To create Button we Jbutton class


rules = new JButton("Rules");
rules.setBounds(735, 270, 120, 25);
rules.setBackground(new Color(30, 144, 254));//for color
EC321: Java Programming 20EC084

rules.setForeground(Color.WHITE);
rules.addActionListener(this);//some action performed on buttons
add(rules);

//To create Button we Jbutton class


back = new JButton("Back");
back.setBounds(915, 270, 120, 25);
back.setBackground(new Color(30, 144, 254));//for color
back.setForeground(Color.WHITE);
back.addActionListener(this);
add(back);

//size of frame
setSize(1200,500); //width and height of frame
setLocation(200,150);
setVisible(true);// to show Frame
}

public void actionPerformed(ActionEvent ae){


if(ae.getSource() == rules){
/*with the help of get text what ever the value is entered by user we acn find ut that*/
String name = tfname.getText();
setVisible(false);//helps to new application
new Rules(name);
} else if (ae.getSource() == back){
setVisible(false);//this helps to close the button

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

OUTPUT
EC321: Java Programming 20EC084

Rules. Java

package quiz.application;

import javax.swing.*;
import java.awt.*;//This import color class
import java.awt.event.*;

public class Rules extends JFrame implements ActionListener{


String name;
JButton start, back;
Rules(String name) {
this.name = name;
getContentPane().setBackground(Color.WHITE);//color of frame
setLayout(null);

JLabel heading = new JLabel("Welcome " + name + " to Simple Minds");


heading.setBounds(50, 20, 700,30);
heading.setFont(new Font("Viner Hand ITC", Font.BOLD,28));
heading.setForeground(new Color(30,144,254));//For color of heading
add(heading);

JLabel rules = new JLabel();


rules.setBounds(20, 90, 700,350);
EC321: Java Programming 20EC084

rules.setFont(new Font("Tahoma", Font.PLAIN,16));


rules.setText(
"<html>"+
"1. You are trained to be a programmer and not a story teller, answer point to
point" + "<br><br>" +
"2. Do not unnecessarily smile at the person sitting next to you, they may also not
know the answer" + "<br><br>" +
"3. You may have lot of options in life but here all the questions are compulsory" +
"<br><br>" +
"4. Crying is allowed but please do so quietly." + "<br><br>" +
"5. Only a fool asks and a wise answers (Be wise, not otherwise)" + "<br><br>" +
"6. Do not get nervous if your friend is answering more questions, may be he/she is
doing Jai Mata Di" + "<br><br>" +
"7. Brace yourself, this paper is not for the faint hearted" + "<br><br>" +
"8. May you know more than what John Snow knows, Good Luck" + "<br><br>"
+//br is for break row
"<html>"

);
add(rules);

//To create Button we Jbutton class


back = new JButton("Back");
back.setBounds(250, 500, 100, 30);
back.setBackground(new Color(30, 144, 254));//for color
back.setForeground(Color.WHITE);
back.addActionListener(this);
add(back);

//To create Button we Jbutton class


start = new JButton("Start");
start.setBounds(400, 500, 100, 30);
start.setBackground(new Color(30, 144, 254));//for color
start.setForeground(Color.WHITE);
start.addActionListener(this);//some action performed on buttons
add( start);

//this creats a frame

setSize(800, 650);
setLocation(350, 100);
setVisible(true);
20EC084
EC321: Java Programming

public void actionPerformed(ActionEvent ae){


if (ae.getSource() == start){
setVisible(false);
new Quiz(name);
}else{
setVisible(false);//to close current frame
new Login();
}
}

public static void main(String[] args){


new Rules("User");

}
}

OUTPUT

Quiz. Java
20EC084
EC321: Java Programming

package quiz.application;

import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Quiz extends JFrame implements ActionListener{

String questions[][] = new String[10][5];


String answers[][] = new String[10][2];
String useranswers[][] = new String[10][1];

JLabel qno, question;


JRadioButton opt1, opt2, opt3, opt4;
ButtonGroup groupoptions;
JButton next, submit, lifeline;

public static int timer = 15;


public static int ans_given = 0;//flag
public static int count = 0;
public static int score = 0;

String name;
Quiz(String name){
this.name = name;
setBounds(40, 0, 1200, 650);
getContentPane().setBackground(Color.WHITE);
setLayout(null);

ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/quiz.jpg"));


JLabel image = new JLabel(i1);
image.setBounds(0, 0, 1200, 300);
add(image);

qno = new JLabel();


qno.setBounds(100, 350, 50, 30);
qno.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(qno);

question = new JLabel();


EC321: Java Programming 20EC084

question.setBounds(150, 350, 900, 30);


question.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(question);

questions[0][0] = "Which is used to find and fix bugs in the Java programs.?";
questions[0][1] = "JVM";
questions[0][2] = "JDB";
questions[0][3] = "JDK";
questions[0][4] = "JRE";

questions[1][0] = "What is the return type of the hashCode() method in the Object
class?";
questions[1][1] = "int";
questions[1][2] = "Object";
questions[1][3] = "long";
questions[1][4] = "void";

questions[2][0] = "Which package contains the Random class?";


questions[2][1] = "java.util package";
questions[2][2] = "java.lang package";
questions[2][3] = "java.awt package";
questions[2][4] = "java.io package";

questions[3][0] = "An interface with no fields or methods is known as?";


questions[3][1] = "Runnable Interface";
questions[3][2] = "Abstract Interface";
questions[3][3] = "Marker Interface";
questions[3][4] = "CharSequence Interface";

questions[4][0] = "In which memory a String is stored, when we create a string using
new operator?";
questions[4][1] = "Stack";
questions[4][2] = "String memory";
questions[4][3] = "Random storage space";
questions[4][4] = "Heap memory";

questions[5][0] = "Which of the following is a marker interface?";


questions[5][1] = "Runnable interface";
questions[5][2] = "Remote interface";
questions[5][3] = "Readable interface";
questions[5][4] = "Result interface";

questions[6][0] = "Which keyword is used for accessing the features of a package?";


questions[6][1] = "import";
EC321: Java Programming 20EC084

questions[6][2] = "package";
questions[6][3] = "extends";
questions[6][4] = "export";

questions[7][0] = "In java, jar stands for?";


questions[7][1] = "Java Archive Runner";
questions[7][2] = "Java Archive";
questions[7][3] = "Java Application Resource";
questions[7][4] = "Java Application Runner";

questions[8][0] = "Which of the following is a mutable class in java?";


questions[8][1] = "java.lang.StringBuilder";
questions[8][2] = "java.lang.Short";
questions[8][3] = "java.lang.Byte";
questions[8][4] = "java.lang.String";

questions[9][0] = "Which of the following option leads to the portability and security of
Java?";
questions[9][1] = "Bytecode is executed by JVM";
questions[9][2] = "The applet makes the Java code secure and portable";
questions[9][3] = "Use of exception handling";
questions[9][4] = "Dynamic binding between objects";

answers[0][1] = "JDB";
answers[1][1] = "int";
answers[2][1] = "java.util package";
answers[3][1] = "Marker Interface";
answers[4][1] = "Heap memory";
answers[5][1] = "Remote interface";
answers[6][1] = "import";
answers[7][1] = "Java Archive";
answers[8][1] = "java.lang.StringBuilder";
answers[9][1] = "Bytecode is executed by JVM";

opt1 = new JRadioButton();


opt1.setBounds(170, 400, 700, 30);
opt1.setBackground(Color.WHITE);
opt1.setFont(new Font("Dialog", Font.PLAIN,20));
add(opt1);

opt2 = new JRadioButton();


opt2.setBounds(170, 440, 700, 30);
opt2.setBackground(Color.WHITE);
opt2.setFont(new Font("Dialog", Font.PLAIN,20));
EC321: Java Programming 20EC084

add(opt2);

opt3 = new JRadioButton();


opt3.setBounds(170, 480, 700, 30);
opt3.setBackground(Color.WHITE);
opt3.setFont(new Font("Dialog", Font.PLAIN,20));
add(opt3);

opt4 = new JRadioButton();


opt4.setBounds(170, 520, 700, 30);
opt4.setBackground(Color.WHITE);
opt4.setFont(new Font("Dialog", Font.PLAIN,20));
add(opt4);

groupoptions = new ButtonGroup();


groupoptions.add(opt1);
groupoptions.add(opt2);
groupoptions.add(opt3);
groupoptions.add(opt4);

//This is for next button


next = new JButton("Next");
next.setBounds(900, 400, 200, 40);
next.setFont(new Font("Tahoma", Font.PLAIN, 22));
next.setBackground(new Color(30, 144, 255));
next.setForeground(Color.WHITE);
next.addActionListener(this);
add(next);

//This is for next button


lifeline = new JButton("50-50 Lifeline");
lifeline.setBounds(900, 450, 200, 40);
lifeline.setFont(new Font("Tahoma", Font.PLAIN, 22));
lifeline.setBackground(new Color(30, 144, 255));
lifeline.setForeground(Color.WHITE);
lifeline.addActionListener(this);
add(lifeline);

//This is for next button


submit = new JButton("Submit");
submit.setBounds(900, 500, 200, 40);
submit.setFont(new Font("Tahoma", Font.PLAIN, 22));
submit.setBackground(new Color(30, 144, 255));
submit.setForeground(Color.WHITE);
EC321: Java Programming 20EC084

submit.addActionListener(this);
submit.setEnabled(false);//to disable submit Button
add(submit);

//function when question start


start(count);//start from quetion 1

setVisible(true);
}

public void actionPerformed(ActionEvent ae){


if(ae.getSource() == next){
repaint();
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);

ans_given = 1;
if (groupoptions.getSelection() == null){
useranswers[count][0] = "";

}else{
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}

if(count == 8){
next.setEnabled(false);
submit.setEnabled(true);
}

count++;
start(count);

}else if(ae.getSource() == lifeline){


if (count == 2 || count == 4 || count == 6 || count == 8 || count ==9 ){
opt2.setEnabled(false);
opt3.setEnabled(false);

}else {
opt1.setEnabled(false);
opt4.setEnabled(false);
EC321: Java Programming 20EC084

}
lifeline.setEnabled(false);//this disable the lifeline buttton after clicking once

}else if (ae.getSource() == submit){


ans_given = 1;

if (groupoptions.getSelection() == null){
useranswers[count][0] = "";

}else{
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
//this loop is for answers to show after submission
for(int i = 0; i < useranswers.length; i++){
if(useranswers[i][0].equals(answers[i][1])){
score += 10;//if ans true then he will get 10
}else{
score+=0;//if ans wrong then he will get 0
}
}
//to calculate total score to display
setVisible(false);
new Score(name, score);

}
}
// for timer
public void paint(Graphics g){
super.paint(g);
String time = " Time left " + timer + " seconds "; //15
g.setColor(Color.red);
g.setFont(new Font("Tahoma", Font.BOLD,25));//to show timer bold

if (timer > 0 ){
g.drawString(time, 850, 400);
}else{
g.drawString("Times up!!", 850, 400);//this show times up
}
timer--;//14

try{
Thread.sleep(1000);
repaint();//repaints the value of seconds
EC321: Java Programming 20EC084

}catch (Exception e){


e.printStackTrace();
}

if (ans_given == 1){
ans_given = 0;
timer = 15;
}else if(timer < 0){
timer = 15;
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);

if(count == 8){
next.setEnabled(false);
submit.setEnabled(true);
}

if(count == 9){//submit button case


if (groupoptions.getSelection() == null){
useranswers[count][0] = "";

}else{
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
//this loop is for answers to show after submission
for(int i = 0; i < useranswers.length; i++){
if(useranswers[i][0].equals(answers[i][1])){
score += 10;//if ans true then he will get 10
}else{
score+=0;//if ans wrong then he will get 0
}
}
//to calculate total score to display
setVisible(false);
new Score(name, score);

}else {//next button case

if (groupoptions.getSelection() == null){
useranswers[count][0] = "";
EC321: Java Programming 20EC084

}else{
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}

count++;//0//1
start(count);
}
}

public void start(int count){


qno.setText("" + (count + 1) + ". ");//Increase count by 1 to next qes
question.setText(questions[count][0]);
opt1.setText(questions[count][1]);
opt1.setActionCommand(questions[count][1]);

opt2.setText(questions[count][2]);
opt2.setActionCommand(questions[count][2]);

opt3.setText(questions[count][3]);
opt3.setActionCommand(questions[count][3]);

opt4.setText(questions[count][4]);
opt4.setActionCommand(questions[count][4]);

groupoptions.clearSelection();
}

public static void main(String[] args){


new Quiz("User");
}

OUTPUT
EC321: Java Programming 20EC084

Score. Java

package quiz.application;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Score extends JFrame implements ActionListener {

Score(String name, int score){


setBounds(400, 150, 750, 550);
getContentPane().setBackground(Color.WHITE);
setLayout(null);

ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/score.png"));


Image i2 = i1.getImage().getScaledInstance(300, 250, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel image = new JLabel(i3);
image.setBounds(0, 200, 300, 250);
add(image);
EC321: Java Programming 20EC084

JLabel heading = new JLabel("Thankyou " + name + " for playing Simple Minds");
heading.setBounds(45, 30, 700, 30);
heading.setFont(new Font("Tahoma", Font.PLAIN, 26));
add(heading);

JLabel lblscore = new JLabel("Your Score is " + score);


lblscore.setBounds(350, 200, 300, 30);
lblscore.setFont(new Font("Tahoma", Font.PLAIN, 26));
add(lblscore);

//This is for next button


JButton submit = new JButton("play Again");
submit.setBounds(380, 270, 120, 30);
submit.setBackground(new Color(30, 144, 255));
submit.setForeground(Color.WHITE);
submit.addActionListener(this);
add(submit);
setVisible(true);//this helps to show the frame

}
public void actionPerformed(ActionEvent ae){
setVisible(false);
new Login();

}
public static void main(String[] args){
new Score("User ", 0);
}

}
OUTPUT
EC321: Java Programming

You might also like