Java_Praticals
Java_Praticals
Program:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
OUTPUT
Experiment:2
Program:
import java.util.Scanner;
scanner.close();
}
}
OUTPUT
Experiment:3
Program:
import java.util.Scanner;
scanner.close();
}
}
OUTPUT
Experiment:4
Program:
import java.util.Scanner;
scanner.close();
}
}
OUTPUT
Experiment:5
Program:
public class SumOfNaturalNumbersEfficient {
public static void main(String[] args) {
int n = 100;
int sum = n * (n + 1) / 2;
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;
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):
");
}
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");
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 "));
}
OUTPUT