Java Programs 2semester
Java Programs 2semester
Part A
*1. Program to find whether the given number is Positive, Negative or Zero.*
If (num > 0) {
System.out.println(num + “ is positive.”);
System.out.println(num + “ is negative.”);
} else {
System.out.println(num + “ is zero.”);
Output:
Enter a number: 10
10 is positive.
```
Public class Main {
System.out.println(“Factorial of “ + I + “ = “ + factorial);
Long factorial = 1;
Factorial *= I;
Return factorial;
Output:
Factorial of 1 = 1
Factorial of 2 = 2
Factorial of 3 = 6
Factorial of 4 = 24
Factorial of 5 = 120
Factorial of 6 = 720
Factorial of 7 = 5040
Factorial of 8 = 40320
Factorial of 9 = 362880
Factorial of 10 = 3628800
*3. Program to demonstrate classes & objects.*
```
This.name = name;
This.age = age;
System.out.println(“Name: “ + name);
System.out.println(“Age: “ + age);
Student1.displayInfo();
Student2.displayInfo();
}
}
Output:
Age: 20
Age: 22
```
System.out.println(calculator.add(10.5, 20.7));
Output:
30
31.2
60
*5. Program to demonstrate single inheritance (simple calculator – base class, Advanced
Calculator-derived class).*
```
If (num2 != 0) {
} else {
Return 0;
System.out.println(calculator.add(10, 20));
System.out.println(calculator.subtract(30, 10));
System.out.println(calculator.multiply(10, 20));
System.out.println(calculator.divide(20, 10));
Output:
30
20
200
2
*6. Program to find Maximum & Minimum element in one dimensional array of numbers.*
```
Import java.util.Arrays;
Arrays.sort(numbers);
Output:
Minimum element: 10
Maximum element: 50
```
If (str.equals(reversedStr)) {
System.out.println(str + “ is a palindrome.”);
} else {
Output:
Madam is a palindrome
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.
```
Public Student(int regNo, String name, int marks1, int marks2, int marks3) {
This.regNo = regNo;
This.name = name;
This.marks1 = marks1;
This.marks2 = marks2;
This.marks3 = marks3;
System.out.println(“Name: “ + name);
System.out.println(“Marks 1: “ + marks1);
System.out.println(“Marks 2: “ + marks2);
System.out.println(“Marks 3: “ + marks3);
Student.displayInfo();
System.out.println();
Output:
```
Student 1:
Marks: 90 80 70
Student 2:
Marks: 95 85 75
Student 3:
Marks: 90 80 70
```
```
Output:
```
At Main.main(Main.java:3)
```
```
```
Output:
```
At Main.main(Main.java:4)
```
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.InputMismatchException;
Import java.util.Scanner;
Try {
Int a = scanner.nextInt();
Int b = scanner.nextInt();
System.out.println(“Sum: “ + (a + b));
} catch (InputMismatchException e) {
```
Output:
```
Abc
```
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 Main() {
morningButton = new Button(“M”);
morningButton.addActionListener(this);
afternoonButton.addActionListener(this);
eveningButton.addActionListener(this);
closeButton.addActionListener(this);
add(morningButton, BorderLayout.NORTH);
add(afternoonButton, BorderLayout.CENTER);
add(eveningButton, BorderLayout.EAST);
add(closeButton, BorderLayout.SOUTH);
setSize(300, 200);
setVisible(true);
If (e.getSource() == morningButton) {
System.out.println(“Good Morning”);
System.out.println(“Good Afternoon”);
System.out.println(“Good Evening”);
} else if (e.getSource() == closeButton) {
System.exit(0);
New Main();
```
Output:
```
Import java.awt.*;
Import java.awt.event.*;
Label label;
Public Main() {
addMouseMotionListener(this);
add(label, BorderLayout.CENTER);
setSize(300, 200);
setVisible(true);
Label.setText(“Mouse Clicked”);
Label.setText(“Mouse Pressed”);
Label.setText(“Mouse Released”);
Label.setText(“Mouse Entered”);
}
Public void mouseExited(MouseEvent e) {
Label.setText(“Mouse Exited”);
Label.setText(“Mouse Dragged”);
Label.setText(“Mouse Moved”);
New Main();
```
Output:
```
Import java.io.*;
Public class Main {
Try {
Dos.writeInt(10);
Dos.writeDouble(20.5);
Dos.writeBoolean(true);
Dos.close();
Fos.close();
System.out.println(“Int: “ + num1);
System.out.println(“Double: “ + num2);
System.out.println(“Boolean: “ + bool);
Dis.close();
Fis.close();
} catch (IOException e) {
System.out.println(“Error: “ + e.getMessage());
```
Output:
Int: 10
Double: 20.5
Boolean: true
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.
```
Import java.awt.*;
Import java.awt.event.*;
fatherButton.addActionListener(this);
motherButton.addActionListener(this);
closeButton.addActionListener(this);
add(fatherButton, BorderLayout.NORTH);
add(motherButton, BorderLayout.CENTER);
add(closeButton, BorderLayout.EAST);
add(fatherNameLabel, BorderLayout.WEST);
add(fatherAgeLabel, BorderLayout.SOUTH);
add(fatherDesignationLabel, BorderLayout.LINE_START);
add(motherNameLabel, BorderLayout.LINE_END);
add(motherAgeLabel, BorderLayout.PAGE_START);
add(motherDesignationLabel, BorderLayout.PAGE_END);
setSize(400, 300);
setVisible(true);
If (e.getSource() == fatherButton) {
System.exit(0);
New Main();
```
Output:
AWT window with three buttons father, mother and close. Displays the respective details of
father and mother as name, age and designation when respective buttons are clicked.
```
Import java.awt.*;
Import java.awt.event.*;
MenuBar menuBar;
Public Main() {
newFileItem.addActionListener(this);
openFileItem.addActionListener(this);
exitItem.addActionListener(this);
cutItem.addActionListener(this);
copyItem.addActionListener(this);
pasteItem.addActionListener(this);
fileMenu.add(newFileItem);
fileMenu.add(openFileItem);
fileMenu.add(exitItem);
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setMenuBar(menuBar);
setSize(400, 300);
setVisible(true);
If (e.getSource() == newFileItem) {
System.out.println(“File opened”);
System.exit(0);
System.out.println(“Text cut”);
System.out.println(“Text copied”);
System.out.println(“Text pasted”);
New Main();
```
Output:
AWT window with menu bar and pull-down menus. Displays messages when menu items
are clicked.