Question

In: Computer Science

this is java code package calculator; import java.util.Scanner; import java.lang.Math; public class Calculator { public static...

this is java code

package calculator;

import java.util.Scanner;

import java.lang.Math;

public class Calculator {

public static void main(String[] args) {

   double numx, numy;

Scanner scanner = new Scanner(System.in);

System.out.println(" Enter an operation :");

System.out.println("1: Addition");

System.out.println("2: Subtraction");

System.out.println("3: Multiplication");

System.out.println("4: Division");

System.out.println("5: Modulus");

System.out.println("6: Power");

System.out.println("7: Square");

System.out.println("8: Factorial");

System.out.println("9: Log");

System.out.println("10: Sin");

System.out.println("11: Absolute value");

System.out.println("12: Average Of Array Elements");

String operator = scanner.next();

System.out.println();

double output;

switch(operator)

{

case "1":

    System.out.print("Enter first number:");

/* We are using data type double so that user

* can enter integer as well as floating point

* value

*/

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

   output = sum( numx, numy);

break;

case "2":

    System.out.print("Enter first number:");

/* We are using data type double so that user

* can enter integer as well as floating point

* value

*/

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

   output = Subtraction( numx, numy);

break;

case "3":

    System.out.print("Enter first number:");

/* We are using data type double so that user

* can enter integer as well as floating point

* value

*/

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

   output = Multiplication( numx,numy);

break;

case "4":

    System.out.print("Enter first number:");

/* We are using data type double so that user

* can enter integer as well as floating point

* value

*/

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

   output = Division( numx, numy );

break;

case "5":

    System.out.print("Enter first number:");

/* We are using data type double so that user

* can enter integer as well as floating point

* value

*/

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

output =Modulus( numx,numy);

   break;

case "6":

    System.out.print("Enter first number:");

numx = scanner.nextDouble();

System.out.print("Enter second number:");

numy = scanner.nextDouble();

   output = power( numx, numy);

   break;

case "7":

   System.out.print("Enter first number:");

numx = scanner.nextDouble();

   output= Square_root( numx);

   break;

case "8":

    System.out.print("Enter first number:");

numx = scanner.nextDouble();

output=factorial( numx);

break;

case "9":

       System.out.print("Enter first number:");

     numx = scanner.nextDouble();

   output=log1( numx); // log function

   break;

case "10":

     System.out.print("Enter first number:");

     numx = scanner.nextDouble();

   output=sin1( numx); //sin finction

   break;

case "11":

     System.out.print("Enter first number:");

     numx = scanner.nextDouble();

   output=absolute( numx); //absolute function

   break;

case "12":

      

  

output= average( );

   break;

/* If user enters any other operator apart from

* then display an error message to user

*

*/

default:

System.out.printf("You have entered wrong operator");

return;

}

System.out.println(output);

}

public static double sum(double numx, double numy)

{

double sum=0;

return sum=numx+numy;

}

public static double Subtraction(double numx, double numy)

{

double sub=0;

return sub=numx-numy;

}

public static double Multiplication(double numx, double numy)

{

double Mul=0;

return Mul=numx*numy;

}

public static double Division(double numx, double numy)

{

double Division=0;

return Division=numx/numy;

}

public static double Modulus(double numx, double numy)

{

double Mod=0;

return Mod=numx%numy;

}

public static double power(double numx, double numy)

{

double output = Math.pow(numx, numy);

return output;

}

public static double Square_root(double numx)

{

double output = Math.sqrt(numx);

return output;

}

public static double factorial(double numx)

{

double factorial=1;  

for(int i = 1; i <= numx; ++i) //loop for calculation of factorial

{

// factorial = factorial * i;

factorial *= i;

}

return factorial;

}

public static double log1(double numx)

{

    double output=1;

return output=Math.log(numx);

}

public static double absolute(double numx)

{

    double output=1;

return output=Math.abs(numx);

}

public static double sin1(double numx)

{

double output=1;

return output=Math.sin(numx);

}

public static double average( )

{

    int n =1;

    Scanner scanner = new Scanner(System.in);

double total = 0;

   System.out.println("Enter size of array");

   n=scanner.nextInt();

   int i=0;

   double[] arr = new double[n];

   while(i<arr.length) // while loop for getting elements in array

   {

       System.out.print("Enter Element No."+(i+1)+": ");

   arr[i] = scanner.nextDouble();

   i++;

   }

   for(int j=0; j<arr.length; j++){ // loop to calculate sum of array

    total = total + arr[j];

}

   double average = total / arr.length;

   return average;

}

}

for this code draw High level flowchart.

Solutions

Expert Solution

Flowchart for the program:


Related Solutions

WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public...
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { int team1[] = new int[4]; int team2[] = new int[4]; for (int qtr = 0; qtr < 4; qtr++) { quarterScoring(team1, team2, qtr); } int team1Total = teamTotal(team1); int team2Total = teamTotal(team2); displayGameResults(team1, team2); if (team1Total > team2Total) { System.out.println("Team 1 has won the game!"); } else { System.out.println("Team 2 has won the game!"); } } static int teamTotal(int[]...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry {...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry { private String month; private int day; private int year; //a four digit number. public void writeOutput() { System.out.println(month + " " + day + ", " + year); } public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); month = keyboard.next(); day = keyboard.nextInt(); year = keyboard.nextInt(); } public int getDay() { return day;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT