Experiment:1
AIM: Write the program in java to print “Hello World”!
Program:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
OUTPUT
Experiment:2
AIM: Write the program in java to Additions, Subtraction,
Multiplication & Division of Two values.
Program:
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first value: ");
double firstValue = scanner.nextDouble();
System.out.print("Enter the second value: ");
double secondValue = scanner.nextDouble();
double sum = firstValue + secondValue;
double difference = firstValue - secondValue;
double product = firstValue * secondValue;
double quotient = firstValue / secondValue;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
scanner.close();
}
}
OUTPUT
Experiment:3
AIM: Write the program in java to find greatest number in among
given 3 numbers.
Program:
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int firstNumber = scanner.nextInt();
System.out.print("Enter the second number: ");
int secondNumber = scanner.nextInt();
System.out.print("Enter the third number: ");
int thirdNumber = scanner.nextInt();
int largestNumber = firstNumber;
if (secondNumber > largestNumber) {
largestNumber = secondNumber;
}
if (thirdNumber > largestNumber) {
largestNumber = thirdNumber;
}
System.out.println("The greatest number is: " + largestNumber);
scanner.close();
}
}
OUTPUT
Experiment:4
AIM: Write the program in java to calculate the marks of five
subjects.
Program:
import java.util.Scanner;
public class CalculateMarks {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the marks for subject 1: ");
int subject1Marks = scanner.nextInt();
System.out.print("Enter the marks for subject 2: ");
int subject2Marks = scanner.nextInt();
System.out.print("Enter the marks for subject 3: ");
int subject3Marks = scanner.nextInt();
System.out.print("Enter the marks for subject 4: ");
int subject4Marks = scanner.nextInt();
System.out.print("Enter the marks for subject 5: ");
int subject5Marks = scanner.nextInt();
int totalMarks = subject1Marks + subject2Marks + subject3Marks
+ subject4Marks + subject5Marks;
float percentage = (totalMarks / 500.0f) * 100.0f;
System.out.println("Total marks: " + totalMarks);
System.out.println("Percentage: " + percentage + "%");
scanner.close();
}
}
OUTPUT
Experiment:5
AIM: Write the program in java to find sum of the N natural
numbers.
Program:
public class SumOfNaturalNumbersEfficient {
public static void main(String[] args) {
int n = 100;
int sum = n * (n + 1) / 2;
System.out.println("Sum of the first " + n + " natural numbers: " + sum);
}
}
OUTPUT
Experiment:6
AIM: Write the program in java to write an analog clock using applet.
Program:
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class analogClock extends Applet {
@Override
public void init()
{
this.setSize(new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
@Override
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}
private void delayAnimation()
{
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g)
{
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.white);
g.fillOval(300, 100, 200, 200);
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
double angle;
int x, y;
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.red);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 80);
y = (int)(Math.sin(angle) * 80);
g.setColor(Color.blue);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 50);
y = (int)(Math.sin(angle) * 50);
g.setColor(Color.black);
g.drawLine(400, 200, 400 + x, 200 - y);
}
}
OUTPUT
Experiment:7
AIM: Write the program in java to develop the scientific calculations
using swing.
Program:
import java.util.Scanner;
public class ScientificCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double result = 0;
boolean exit = false;
boolean newCalculation = true;
while (!exit) {
if (newCalculation) {
System.out.println("Scientific Calculator Menu:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Square Root");
System.out.println("6. Exponentiation");
System.out.println("7. Sine");
System.out.println("8. Cosine");
System.out.println("9. Tangent");
System.out.println("10. Logarithm (base 10)");
System.out.println("11. Natural Logarithm (ln)");
System.out.println("12. Memory Operations");
System.out.println("13. Exit");
System.out.print("Select an operation (1-13): ");
} else {
System.out.println("Result: " + result);
System.out.print("Select an operation (1-13 or 0 for new calculation):
");
}
int choice = scanner.nextInt();
double operand;
switch (choice) {
case 0:
newCalculation = true;
break;
case 1:
System.out.print("Enter the number to add: ");
operand = scanner.nextDouble();
result += operand;
newCalculation = false;
break;
case 2:
System.out.print("Enter the number to subtract: ");
operand = scanner.nextDouble();
result -= operand;
newCalculation = false;
break;
case 3:
System.out.print("Enter the number to multiply by: ");
operand = scanner.nextDouble();
result *= operand;
newCalculation = false;
break;
case 4:
System.out.print("Enter the number to divide by: ");
operand = scanner.nextDouble();
if (operand != 0) {
result /= operand;
} else {
System.out.println("Error: Division by zero.");
}
newCalculation = false;
break;
case 5:
result = Math.sqrt(result);
newCalculation = false;
break;
case 6:
System.out.print("Enter the exponent: ");
operand = scanner.nextDouble();
result = Math.pow(result, operand);
newCalculation = false;
break;
case 7:
result = Math.sin(result);
newCalculation = false;
break;
case 8:
result = Math.cos(result);
newCalculation = false;
break;
case 9:
result = Math.tan(result);
newCalculation = false;
break;
case 10:
if (result > 0) {
result = Math.log10(result);
} else {
System.out.println("Error: Invalid input for logarithm.");
}
newCalculation = false;
break;
case 11:
if (result > 0) {
result = Math.log(result);
} else {
System.out.println("Error: Invalid input for natural logarithm.");
}
newCalculation = false;
break;
case 12:
System.out.println("Memory Menu:");
System.out.println("1. Store result in memory");
System.out.println("2. Recall memory");
System.out.println("3. Clear memory");
System.out.print("Select a memory operation (1-3): ");
int memoryChoice = scanner.nextInt();
switch (memoryChoice) {
case 1:
memory = result;
break;
case 2:
result = memory;
break;
case 3:
memory = 0;
break;
default:
System.out.println("Invalid memory operation.");
break;
}
break;
case 13:
exit = true;
break;
default:
System.out.println("Invalid choice.");
break;
}
}
System.out.println("Calculator closed.");
}
}
OUTPUT
Experiment:8
AIM: Write the program in java to create an editor like MS-word
using swing.
Program:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BuiltinStyle;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
class GFG {
public static void main(String[] args)
{
Document document = new Document();
Section section = document.addSection();
Paragraph heading = section.addParagraph();
heading.appendText("Java");
Paragraph subheading_1 = section.addParagraph();
subheading_1.appendText("What's Java");
Paragraph para_1 = section.addParagraph();
para_1.appendText(
"Java is a general purpose, high-level programming language
developed by Sun Microsystems."
+ " The Java programming language was developed by a
small team of engineers, "
+ "known as the Green Team, who initiated the language in
1991.");
Paragraph para_2 = section.addParagraph();
para_2.appendText(
"Originally called OAK, the Java language was designed for
handheld devices and set-top boxes. "
+ "Oak was unsuccessful and in 1995 Sun changed the name
to Java and modified the language to take "
+ "advantage of the burgeoning World Wide Web. ");
// Adding another subheading
Paragraph subheading_2 = section.addParagraph();
subheading_2.appendText("Java Today");
Paragraph para_3 = section.addParagraph();
para_3.appendText(
"Today the Java platform is a commonly used foundation for
developing and delivering content "
+ "on the web. According to Oracle, there are more than 9
million Java developers worldwide and more "
+ "than 3 billion mobile phones run Java.");
heading.applyStyle(BuiltinStyle.Title);
subheading_1.applyStyle(BuiltinStyle.Heading_3);
subheading_2.applyStyle(BuiltinStyle.Heading_3);
ParagraphStyle style = new ParagraphStyle(document);
style.setName("paraStyle");
style.getCharacterFormat().setFontName("Arial");
style.getCharacterFormat().setFontSize(11f);
document.getStyles().add(style);
para_1.applyStyle("paraStyle");
para_2.applyStyle("paraStyle");
para_3.applyStyle("paraStyle");
for (int i = 0;
i < section.getParagraphs().getCount(); i++) {
section.getParagraphs()
.get(i)
.getFormat()
.setAfterAutoSpacing(true);
}
document.saveToFile(
"output/CreateAWordDocument.docx",
FileFormat.Docx);
}
}
OUTPUT
Experiment:9
AIM: Write the program in java to first character uppercase in a
sentence.
Program:
public class FirstLetterCapital1
{
public static void main(String args[])
{
System.out.println(capitalize("this pratical is made by Om Yadav "));
}
public static final String capitalize(String str)
{
if (str == null || str.length() == 0) return str;
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}
OUTPUT
Experiment:10
AIM: Write the program in java to find user defined custom
exception.
Program:
public class TestCustomException {
public static void main(String[] args) throws MyCustomException {
try {
validateInput();
} catch (MyCustomException e) {
System.out.println(e.getMessage());
}
}
private static void validateInput() throws MyCustomException {
String input = "invalid input";
if (!input.equals("valid input")) {
throw new MyCustomException("Invalid input!");
}
}
}
class MyCustomException extends Exception {
public MyCustomException(String message) {
super(message); }
}
OUTPUT