Experiment:1
AIM: Write the program in java to print “Hello World”!
Program:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
OUTPUT
Experiment:2
AIM: Write the program in java to Additions, Subtraction,
Multiplication & Division of Two values.
Program:
import [Link];
public class ArithmeticOperations {
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first value: ");
double firstValue = [Link]();
[Link]("Enter the second value: ");
double secondValue = [Link]();
double sum = firstValue + secondValue;
double difference = firstValue - secondValue;
double product = firstValue * secondValue;
double quotient = firstValue / secondValue;
[Link]("Sum: " + sum);
[Link]("Difference: " + difference);
[Link]("Product: " + product);
[Link]("Quotient: " + quotient);
[Link]();
}
}
OUTPUT
Experiment:3
AIM: Write the program in java to find greatest number in among
given 3 numbers.
Program:
import [Link];
public class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number: ");
int firstNumber = [Link]();
[Link]("Enter the second number: ");
int secondNumber = [Link]();
[Link]("Enter the third number: ");
int thirdNumber = [Link]();
int largestNumber = firstNumber;
if (secondNumber > largestNumber) {
largestNumber = secondNumber;
}
if (thirdNumber > largestNumber) {
largestNumber = thirdNumber;
}
[Link]("The greatest number is: " + largestNumber);
[Link]();
}
}
OUTPUT
Experiment:4
AIM: Write the program in java to calculate the marks of five
subjects.
Program:
import [Link];
public class CalculateMarks {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the marks for subject 1: ");
int subject1Marks = [Link]();
[Link]("Enter the marks for subject 2: ");
int subject2Marks = [Link]();
[Link]("Enter the marks for subject 3: ");
int subject3Marks = [Link]();
[Link]("Enter the marks for subject 4: ");
int subject4Marks = [Link]();
[Link]("Enter the marks for subject 5: ");
int subject5Marks = [Link]();
int totalMarks = subject1Marks + subject2Marks + subject3Marks
+ subject4Marks + subject5Marks;
float percentage = (totalMarks / 500.0f) * 100.0f;
[Link]("Total marks: " + totalMarks);
[Link]("Percentage: " + percentage + "%");
[Link]();
}
}
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;
[Link]("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 [Link];
import [Link].*;
import [Link].*;
public class analogClock extends Applet {
@Override
public void init()
{
[Link](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 {
[Link](1000);
}
catch (InterruptedException e) {
[Link]();
}
}
@Override
public void paint(Graphics g)
{
Calendar time = [Link]();
int hour = [Link](Calendar.HOUR_OF_DAY);
int minute = [Link]([Link]);
int second = [Link]([Link]);
if (hour > 12) {
hour -= 12;
}
[Link]([Link]);
[Link](300, 100, 200, 200);
[Link]([Link]);
[Link]("12", 390, 120);
[Link]("9", 310, 200);
[Link]("6", 400, 290);
[Link]("3", 480, 200);
double angle;
int x, y;
angle = [Link]((15 - second) * 6);
x = (int)([Link](angle) * 100);
y = (int)([Link](angle) * 100);
[Link]([Link]);
[Link](400, 200, 400 + x, 200 - y);
angle = [Link]((15 - minute) * 6);
x = (int)([Link](angle) * 80);
y = (int)([Link](angle) * 80);
[Link]([Link]);
[Link](400, 200, 400 + x, 200 - y);
angle = [Link]((15 - (hour * 5)) * 6);
x = (int)([Link](angle) * 50);
y = (int)([Link](angle) * 50);
[Link]([Link]);
[Link](400, 200, 400 + x, 200 - y);
}
}
OUTPUT
Experiment:7
AIM: Write the program in java to develop the scientific calculations
using swing.
Program:
import [Link];
public class ScientificCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double result = 0;
boolean exit = false;
boolean newCalculation = true;
while (!exit) {
if (newCalculation) {
[Link]("Scientific Calculator Menu:");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Square Root");
[Link]("6. Exponentiation");
[Link]("7. Sine");
[Link]("8. Cosine");
[Link]("9. Tangent");
[Link]("10. Logarithm (base 10)");
[Link]("11. Natural Logarithm (ln)");
[Link]("12. Memory Operations");
[Link]("13. Exit");
[Link]("Select an operation (1-13): ");
} else {
[Link]("Result: " + result);
[Link]("Select an operation (1-13 or 0 for new calculation):
");
}
int choice = [Link]();
double operand;
switch (choice) {
case 0:
newCalculation = true;
break;
case 1:
[Link]("Enter the number to add: ");
operand = [Link]();
result += operand;
newCalculation = false;
break;
case 2:
[Link]("Enter the number to subtract: ");
operand = [Link]();
result -= operand;
newCalculation = false;
break;
case 3:
[Link]("Enter the number to multiply by: ");
operand = [Link]();
result *= operand;
newCalculation = false;
break;
case 4:
[Link]("Enter the number to divide by: ");
operand = [Link]();
if (operand != 0) {
result /= operand;
} else {
[Link]("Error: Division by zero.");
}
newCalculation = false;
break;
case 5:
result = [Link](result);
newCalculation = false;
break;
case 6:
[Link]("Enter the exponent: ");
operand = [Link]();
result = [Link](result, operand);
newCalculation = false;
break;
case 7:
result = [Link](result);
newCalculation = false;
break;
case 8:
result = [Link](result);
newCalculation = false;
break;
case 9:
result = [Link](result);
newCalculation = false;
break;
case 10:
if (result > 0) {
result = Math.log10(result);
} else {
[Link]("Error: Invalid input for logarithm.");
}
newCalculation = false;
break;
case 11:
if (result > 0) {
result = [Link](result);
} else {
[Link]("Error: Invalid input for natural logarithm.");
}
newCalculation = false;
break;
case 12:
[Link]("Memory Menu:");
[Link]("1. Store result in memory");
[Link]("2. Recall memory");
[Link]("3. Clear memory");
[Link]("Select a memory operation (1-3): ");
int memoryChoice = [Link]();
switch (memoryChoice) {
case 1:
memory = result;
break;
case 2:
result = memory;
break;
case 3:
memory = 0;
break;
default:
[Link]("Invalid memory operation.");
break;
}
break;
case 13:
exit = true;
break;
default:
[Link]("Invalid choice.");
break;
}
}
[Link]("Calculator closed.");
}
}
OUTPUT
Experiment:8
AIM: Write the program in java to create an editor like MS-word
using swing.
Program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class GFG {
public static void main(String[] args)
{
Document document = new Document();
Section section = [Link]();
Paragraph heading = [Link]();
[Link]("Java");
Paragraph subheading_1 = [Link]();
subheading_1.appendText("What's Java");
Paragraph para_1 = [Link]();
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 = [Link]();
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 = [Link]();
subheading_2.appendText("Java Today");
Paragraph para_3 = [Link]();
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.");
[Link]([Link]);
subheading_1.applyStyle(BuiltinStyle.Heading_3);
subheading_2.applyStyle(BuiltinStyle.Heading_3);
ParagraphStyle style = new ParagraphStyle(document);
[Link]("paraStyle");
[Link]().setFontName("Arial");
[Link]().setFontSize(11f);
[Link]().add(style);
para_1.applyStyle("paraStyle");
para_2.applyStyle("paraStyle");
para_3.applyStyle("paraStyle");
for (int i = 0;
i < [Link]().getCount(); i++) {
[Link]()
.get(i)
.getFormat()
.setAfterAutoSpacing(true);
}
[Link](
"output/[Link]",
[Link]);
}
}
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[])
{
[Link](capitalize("this pratical is made by Om Yadav "));
}
public static final String capitalize(String str)
{
if (str == null || [Link]() == 0) return str;
return [Link](0, 1).toUpperCase() + [Link](1);
}
}
OUTPUT