Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Calculator #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions padmini/Calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.awt.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
Frame f=new Frame();
Label l1 = new Label("First Number");
Label l2 = new Label("Second Number");
Label l3 = new Label("Result");
TextField t1= new TextField();
TextField t2= new TextField();
TextField t3= new TextField();
Button b1 = new Button("Add");
Button b2 = new Button("Sub");
Button b3 = new Button("Mul");
Button b4 = new Button("Div");
Button b5 = new Button("Cancel");
Calculator()
{
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,150,20);
t2.setBounds(200,140,150,20);
t3.setBounds(200,180,150,20);
b1.setBounds(50,250,70,20);
b2.setBounds(110,250,70,20);
b3.setBounds(170,250,70,20);
b4.setBounds(230,250,70,20);
b5.setBounds(290,250,70,20);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
}
public void actionPerformed(ActionEvent e)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String [] args)
{
new Calculator();
}
}
35 changes: 35 additions & 0 deletions padmini/Number Guessing/Number Guessing Game
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.*;
import java.util.Random;
public class NumberGuessing {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I have selected a number between 1 and 100. Try to guess it!");
int maxAttempts = 10;
int secretNumber = random.nextInt(100) + 1;
int attempts = 0;
boolean guessedCorrectly = false;
while (attempts < maxAttempts) {
System.out.print("Enter your guess (between 1 and 100): ");
int guess = scanner.nextInt();
attempts++;
if (guess == secretNumber) {
guessedCorrectly = true;
break;
} else if (guess < secretNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}

if (guessedCorrectly) {
System.out.println("Congratulations! You've guessed the number " + secretNumber + " correctly in " + attempts + " attempts!");
} else {
System.out.println("Sorry, you've exceeded the maximum number of attempts. The correct number was: " + secretNumber);
}

scanner.close();
}
}
79 changes: 79 additions & 0 deletions padmini/SimpleCalculator/Calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.awt.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
Frame f=new Frame();
Label l1 = new Label("First Number");
Label l2 = new Label("Second Number");
Label l3 = new Label("Result");
TextField t1= new TextField();
TextField t2= new TextField();
TextField t3= new TextField();
Button b1 = new Button("Add");
Button b2 = new Button("Sub");
Button b3 = new Button("Mul");
Button b4 = new Button("Div");
Button b5 = new Button("Cancel");
Calculator()
{
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,150,20);
t2.setBounds(200,140,150,20);
t3.setBounds(200,180,150,20);
b1.setBounds(50,250,70,20);
b2.setBounds(110,250,70,20);
b3.setBounds(170,250,70,20);
b4.setBounds(230,250,70,20);
b5.setBounds(290,250,70,20);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
}
public void actionPerformed(ActionEvent e)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String [] args)
{
new Calculator();
}
}
100 changes: 100 additions & 0 deletions padmini/Temperature Converter/Temperature Converter Program
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.util.Scanner;

public class Temp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to Temperature Converter!");
System.out.println("Enter the temperature scale you want to convert from:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.println("3. Kelvin");

System.out.print("Enter your choice (1/2/3): ");
int choiceFrom = scanner.nextInt();

System.out.print("Enter the temperature value: ");
double temperature = scanner.nextDouble();

double convertedTemperature = 0;

switch (choiceFrom) {
case 1:
System.out.println("Convert Celsius to:");
System.out.println("1. Fahrenheit");
System.out.println("2. Kelvin");
System.out.print("Enter your choice (1/2): ");
int choiceToCelsius = scanner.nextInt();
if (choiceToCelsius == 1) {
convertedTemperature = celsiusToFahrenheit(temperature);
System.out.println(temperature + " Celsius = " + convertedTemperature + " Fahrenheit");
} else if (choiceToCelsius == 2) {
convertedTemperature = celsiusToKelvin(temperature);
System.out.println(temperature + " Celsius = " + convertedTemperature + " Kelvin");
} else {
System.out.println("Invalid choice!");
}
break;
case 2:
System.out.println("Convert Fahrenheit to:");
System.out.println("1. Celsius");
System.out.println("2. Kelvin");
System.out.print("Enter your choice (1/2): ");
int choiceToFahrenheit = scanner.nextInt();
if (choiceToFahrenheit == 1) {
convertedTemperature = fahrenheitToCelsius(temperature);
System.out.println(temperature + " Fahrenheit = " + convertedTemperature + " Celsius");
} else if (choiceToFahrenheit == 2) {
convertedTemperature = fahrenheitToKelvin(temperature);
System.out.println(temperature + " Fahrenheit = " + convertedTemperature + " Kelvin");
} else {
System.out.println("Invalid choice!");
}
break;
case 3:
System.out.println("Convert Kelvin to:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.print("Enter your choice (1/2): ");
int choiceToKelvin = scanner.nextInt();
if (choiceToKelvin == 1) {
convertedTemperature = kelvinToCelsius(temperature);
System.out.println(temperature + " Kelvin = " + convertedTemperature + " Celsius");
} else if (choiceToKelvin == 2) {
convertedTemperature = kelvinToFahrenheit(temperature);
System.out.println(temperature + " Kelvin = " + convertedTemperature + " Fahrenheit");
} else {
System.out.println("Invalid choice!");
}
break;
default:
System.out.println("Invalid choice!");
}

scanner.close();
}

public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}

public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}

public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}

public static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheit + 459.67) * 5 / 9;
}

public static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}

public static double kelvinToFahrenheit(double kelvin) {
return (kelvin * 9 / 5) - 459.67;
}
}
Loading