Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

JAVA LAB MANUAL1

The SEP Java Lab Manual outlines a series of programming exercises divided into two parts. Part A includes tasks such as checking number positivity, calculating factorials, demonstrating classes and inheritance, and creating a student class. Part B focuses on exception handling, AWT GUI creation, and file I/O operations.

Uploaded by

divyashreer58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JAVA LAB MANUAL1

The SEP Java Lab Manual outlines a series of programming exercises divided into two parts. Part A includes tasks such as checking number positivity, calculating factorials, demonstrating classes and inheritance, and creating a student class. Part B focuses on exception handling, AWT GUI creation, and file I/O operations.

Uploaded by

divyashreer58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SEP JAVA LAB MANUAL

Laboratory Program List


Part A:
1. Program to find whether the given number is Positive, Negative or Zero.
2. Program to list the factorial of the numbers 1 to 10.
3. Program to demonstrate classes & objects.
4. Program to demonstrate method overloading.
5. Program to demonstrate single inheritance (simple calculator – base class,
Advanced Calculator – derived class).
6. Program to find Maximum & Minimum element in one dimensional array of
numbers.
7. Program to check whether the given string is palindrome or not.
8. Program to create a ‘Student’ class with Reg.no., name and marks of 3 subjects.
Calculate the total marks of 3 subjects and create an array of 3 student objects &
display the results.

Part B:

1. Program to generate negative array size exception


2. Program to generate NullPointer Exception.
3. Program that reads two integer numbers for the variables a and b. The program
should catch NumberFormatException & display the error message.
4. Program to create AWT window with 4 buttons M/A/E/Close. Display M for
Good Morning, A for Afternoon, E for evening and Close button to exit the
window. 5. Program to demonstrate the various mouse handling events.
6. Program to read and write Binary I/O file.
7. Program to create window with three buttons father, mother and close. Display
the respective details of father and mother as name, age and designation using
AWT controls.
8. Program to create menu bar and pull-down menus

part A

1.program to check whether the given number is Positive, Negative, or Zero.

import java.util.Scanner;
public class NumberCheck
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0)
{
System.out.println("Positive");
}
else if (number < 0)
{
System.out.println("Negative");
}
else
{
System.out.println("Zero");
}
SEP JAVA LAB MANUAL

}
}
Output:

2. Program to list the factorial of the number from 1 to 10


public class FactorialList
{
public static void main(String[] args)
{
for (int number = 1; number <= 10; number++)
{
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
}
}

Output:

3. Program to demonstrate classes & objects.


class Car
{
String brand;
int year;
void displayDetails()
{
System.out.println("Brand: " + brand);
System.out.println("Year: " + year);
}
SEP JAVA LAB MANUAL

}
public class Sample
{
public static void main(String[] args)
{
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.displayDetails();
}
}

Output

4. program to demonstrate method overloading

class Calculator
{
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
}
public class Pgm4
{
public static void main(String[] args)
{
Calculator calc = new Calculator();
System.out.println("Sum of 10 and 20: " + calc.add(10, 20));
System.out.println("Sum of 10, 20, and 30: " + calc.add(10, 20, 30));
}}

5. Program to demonstrate single inheritance (simple calculator – base class,


Advanced Calculator – derived class).
SEP JAVA LAB MANUAL

class BaseCalculator
{
int add(int a, int b)
{
return a + b;
}
}

class AdvancedCalculator extends BaseCalculator


{
int multiply(int a, int b)
{
return a * b;
}
}
public class Pgm5
{
public static void main(String[] args)
{
AdvancedCalculator calc = new AdvancedCalculator();
System.out.println("Addition: " + calc.add(10, 20));
System.out.println("Multiplication: " + calc.multiply(10, 20));
}
}

6. Program to find Maximum & Minimum element in a one-dimensional array of


numbers.

public class MaxMinArray


{
public static void main(String[] args)
{
int[] numbers = {10, 20, 4, 45, 99, 2,67};
int max = numbers[0];
int min = numbers[0];
for (int i = 1; i < numbers.length; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}
if (numbers[i] < min)
{
min = numbers[i];
}
}
SEP JAVA LAB MANUAL

System.out.println("Maximum Element: " + max);


System.out.println("Minimum Element: " + min);
}
}

7. Program to check whether the given string is palindrome or not.

import java.util.Scanner;
public class PalindromeCheck
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed))
{
System.out.println("The string is a palindrome.");
}
else
{
System.out.println("The string is not a palindrome.");
}
scanner.close();
}
}

8. Program to create a ‘Student’ class with Reg. no., name, and marks of 3
subjects. Calculate the total marks of 3 subjects and create an array of 3 student
objects & display the results.

import java.util.Scanner;

class Student
{
int regNo;
SEP JAVA LAB MANUAL

String name;
int marks1, marks2, marks3;
int getTotalMarks()
{
return marks1 + marks2 + marks3;
}
}
public class Pgm8
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[3];
for (int i = 0; i < 3; i++)
{
students[i] = new Student();
System.out.println("Enter details for Student " + (i + 1));
System.out.print("Enter Registration Number: ");
students[i].regNo = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
students[i].name = scanner.nextLine();
System.out.print("Enter Marks for Subject 1: ");
students[i].marks1 = scanner.nextInt();
System.out.print("Enter Marks for Subject 2: ");
students[i].marks2 = scanner.nextInt();
System.out.print("Enter Marks for Subject 3: ");
students[i].marks3 = scanner.nextInt();
System.out.println();
}
System.out.println("Student Results:");
for (int i = 0; i < 3; i++) {
System.out.println("Student " + (i + 1) + ": " + students[i].name);
System.out.println("Registration No: " + students[i].regNo);
System.out.println("Total Marks: " + students[i].getTotalMarks());
System.out.println();
}
scanner.close();
}
}
SEP JAVA LAB MANUAL

PART B

1 Program to generate negative array size exception

public class NegativeArraySizeExceptionExample


{
public static void main(String[] args)
{
try
{
int[] arr = new int[-5];
}
catch (NegativeArraySizeException e)
{
System.out.println("Exception: " + e);
}
}
}

2.Program to generate NullPointer Exception.

public class NullPointerExample


{
public static void main(String[] args)
SEP JAVA LAB MANUAL

{
String str = null;
System.out.println(str.length());
}
}

3.Program that reads two integer numbers for the variables a and b. The program
should catch NumberFormatException & display the error message.

import java.util.Scanner;

public class NumberFormatExceptionExample


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
try
{
System.out.print("Enter the first number (a): ");
int a = Integer.parseInt(scanner.nextLine());
System.out.print("Enter the second number (b): ");
int b = Integer.parseInt(scanner.nextLine());
System.out.println("You entered: a = " + a + ", b = " + b);
}
catch (NumberFormatException e)
{
System.out.println("Error: Please enter valid integers.");
}
scanner.close();
}
}

4. Program to create AWT window with 4 buttons M/A/E/Close. Display M for


Good Morning, A for Afternoon, E for Evening, and Close button to exit the
window.
import java.awt.*;
import java.awt.event.*;
public class SimpleAWTExample
{
public static void main(String[] args)
{
SEP JAVA LAB MANUAL

Frame frame = new Frame("AWT Buttons Example");


Button btnMorning = new Button("M");
Button btnAfternoon = new Button("A");
Button btnEvening = new Button("E");
Button btnClose = new Button("Close");
frame.setLayout(new FlowLayout());
frame.add(btnMorning);
frame.add(btnAfternoon);
frame.add(btnEvening);
frame.add(btnClose);
btnMorning.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Good Morning");
}
});

btnAfternoon.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Good Afternoon");
}
});

btnEvening.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Good Evening");
}
});

btnClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
frame.setSize(300, 200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
SEP JAVA LAB MANUAL

5. Program to demonstrate various mouse handling events.

import java.awt.*;
import java.awt.event.*;
public class MouseEventExample extends Frame
{
public MouseEventExample()
{
setTitle("Mouse Event Demo");
setSize(300, 200);
setLayout(new FlowLayout());
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at: " + e.getX() + ", " + e.getY());
}
public void mousePressed(MouseEvent e)
{
System.out.println("Mouse Pressed at: " + e.getX() + ", " + e.getY());
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Mouse Released at: " + e.getX() + ", " + e.getY());
}
});

addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
System.out.println("Mouse Moved at: " + e.getX() + ", " + e.getY());
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
SEP JAVA LAB MANUAL

System.exit(0);
}
});

setVisible(true);
}

public static void main(String[] args)


{
new MouseEventExample();
}
}

6.Program to read and write Binary I/O file.

import java.io.*;
public class BinaryFileExample
{
public static void main(String[] args)
{
byte[] data = {10, 20, 30, 40, 50};
try(FileOutputStream fos = new FileOutputStream("data.bin"))
{
fos.write(data);
System.out.println("Data written to the file.");
}
catch (IOException e)
{
System.out.println("Error writing to the file: " + e.getMessage());
}
try(FileInputStream fis = new FileInputStream("data.bin"))
{
int byteRead;
System.out.print("Data read from the file: ");
while ((byteRead = fis.read()) != -1)
{
System.out.print(byteRead + " ");
}
System.out.println();
SEP JAVA LAB MANUAL

} catch (IOException e)
{
System.out.println("Error reading from the file: " + e.getMessage());
}
}
}

7. Program to create a window with three buttons (Father, Mother, Close). Display
the respective details of father and mother (name, age, and designation) using
AWT controls.

import java.awt.*;
import java.awt.event.*;
public class FamilyDetails extends Frame
{
public FamilyDetails()
{
setTitle("Family Details") ;
setSize(400, 200);
setLayout(new FlowLayout());
Button btnFather = new Button("Father");
Button btnMother = new Button("Mother");
Button btnClose = new Button("Close");

Label lblDetails = new Label("");


lblDetails.setAlignment(Label.CENTER);
lblDetails.setPreferredSize(new Dimension(350, 40));
add(btnFather);
add(btnMother);
add(btnClose);
add(lblDetails);
btnFather.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
lblDetails.setText("Father: John, Age: 45, Designation: Engineer");
}
});
btnMother.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
lblDetails.setText("Mother: Mary, Age: 40, Designation: Teacher");
}
});
btnClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
SEP JAVA LAB MANUAL

});

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
setVisible(true);
}

public static void main(String[] args)


{
new FamilyDetails();
}
}

8. Program to create menu bar and pull-down menus

import java.awt.*;
class MenuExample
{
MenuExample()
{
Frame f=new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
SEP JAVA LAB MANUAL

public static void main(String args[])


{
new MenuExample();
}
}

output

You might also like