Telegram Group & Telegram Channel
11. Program to Create a Simple Calculator.
import java.util.Scanner;
import java.io.*;
class Calculator
{
public static void main(String[] args)
{
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try
{
out = new PrintStream("calclog.txt");
do
{
System.out.println("Calculator Program"); System.out.println("--------------------\n");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99)
{
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice)
{
case 1:
sum = calc.add(x, y);
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try
{
sum = calc.div(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "/" + y + "=" + sum);
}
catch (Exception e)
{
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y)
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
input.close();
System.out.println("Ending program...");
}
catch (Exception e){
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}
public int add(int num1, int num2)
{int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{ int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{ int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{int sum = 1;
if (exp == 0)
{sum = 1;
}
while (exp > 0)
{sum = sum * base;
exp--;
}
return sum;
}
}
@java_codings



tg-me.com/java_codings/19
Create:
Last Update:

11. Program to Create a Simple Calculator.
import java.util.Scanner;
import java.io.*;
class Calculator
{
public static void main(String[] args)
{
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try
{
out = new PrintStream("calclog.txt");
do
{
System.out.println("Calculator Program"); System.out.println("--------------------\n");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99)
{
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice)
{
case 1:
sum = calc.add(x, y);
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try
{
sum = calc.div(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "/" + y + "=" + sum);
}
catch (Exception e)
{
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y)
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
input.close();
System.out.println("Ending program...");
}
catch (Exception e){
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}
public int add(int num1, int num2)
{int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{ int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{ int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{int sum = 1;
if (exp == 0)
{sum = 1;
}
while (exp > 0)
{sum = sum * base;
exp--;
}
return sum;
}
}
@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/java_codings/19

View MORE
Open in Telegram


Advance Java ‍ Telegram | DID YOU KNOW?

Date: |

Spiking bond yields driving sharp losses in tech stocks

A spike in interest rates since the start of the year has accelerated a rotation out of high-growth technology stocks and into value stocks poised to benefit from a reopening of the economy. The Nasdaq has fallen more than 10% over the past month as the Dow has soared to record highs, with a spike in the 10-year US Treasury yield acting as the main catalyst. It recently surged to a cycle high of more than 1.60% after starting the year below 1%. But according to Jim Paulsen, the Leuthold Group's chief investment strategist, rising interest rates do not represent a long-term threat to the stock market. Paulsen expects the 10-year yield to cross 2% by the end of the year. A spike in interest rates and its impact on the stock market depends on the economic backdrop, according to Paulsen. Rising interest rates amid a strengthening economy "may prove no challenge at all for stocks," Paulsen said.

That growth environment will include rising inflation and interest rates. Those upward shifts naturally accompany healthy growth periods as the demand for resources, products and services rise. Importantly, the Federal Reserve has laid out the rationale for not interfering with that natural growth transition.It's not exactly a fad, but there is a widespread willingness to pay up for a growth story. Classic fundamental analysis takes a back seat. Even negative earnings are ignored. In fact, positive earnings seem to be a limiting measure, producing the question, "Is that all you've got?" The preference is a vision of untold riches when the exciting story plays out as expected.

Advance Java ‍ from ca


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM USA