Question

In: Computer Science

11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named...

11.1 Simple Arithmetic Program

Using the instructions from Week 1 Lab, create a new folder named Project01. In this folder create a new class named Project01. This class must be in the default package. Make sure that in the comments at the top of the Java program you put your name and today's date using the format for Java comments given in the Week 1 Lab.

For this lab, you will write a Java program to prompt the user to enter two integers. Your program will display a series of arithmetic operations using those two integers. Create a new Java program named Project01.java for this problem.

Sample Output: This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Make sure your output looks EXACTLY like the output below, including spacing. Items in bold are elements input by the user, not hard-coded into the program.

Enter the first number: 12
Enter the second number: 3
12 + 3 = 15
12 - 3 = 9
12 * 3 = 36
12 / 3 = 4
12 % 3 = 0
The average of your two numbers is: 7

A second run of your program with different inputs might look like this:

Enter the first number: -4
Enter the second number: 3
-4 + 3 = -1
-4 - 3 = -7
-4 * 3 = -12
-4 / 3 = -1
-4 % 3 = -1
The average of your two numbers is: 0

HINT: You can start by retyping the code that was given to you in Exercise 3 of ClosedLab01. That code takes in a single number and performs a few arithmetic operations on it. How can you modify that code to take in two numbers? How can you modify it to display "number * number =" instead of "Your number squared is: "? Take it step by step and change one thing at a time.

You can use the following as a template to get you started. Note that you must create your class in the default package and your project must be named Project01.java for the autograder to be able to test it when you submit it.

Solutions

Expert Solution

The answer to this question is as follows:

The code is as follows:

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
int a,b;//Decalring the variables
Scanner sc=new Scanner(System.in);
System.out.print("Enter the First number: ");
a=sc.nextInt();//Reading the first number
System.out.println(a);
System.out.print("Enter the Second number: ");
b=sc.nextInt();//Reading the second number
System.out.println(b);
System.out.println(a+" "+"+"+" "+b+" "+"="+" "+(a+b));//printing the arithmetic operation of a+b
System.out.println(a+" "+"-"+" "+b+" "+"="+" "+(a-b));//printing the arithmetic operation of a-b
System.out.println(a+" "+"*"+" "+b+" "+"="+" "+(a*b));//printing the arithmetic operation of a*b
System.out.println(a+" "+"/"+" "+b+" "+"="+" "+(a/b));//printing the arithmetic operation of a/b
System.out.println(a+" "+"%"+" "+b+" "+"="+" "+(a%b));//printing the arithmetic operation of a%b
System.out.println("The average of your two numbers is: "+(a+b)/2);
  
}
}

For Multiple Files, Custom Library and File Read/Write, use our new - Advanced Java IDE import java.util.Scanner; 2 public class MyClass { 1 public static void main(String args[]) { int a,b;//Decalring the variables Scanner sc=new Scanner (System.in); System.out.print("Enter the First number: "); a-sc.nextInt();//Reading the first number System.out.println(a); System.out.print("Enter the second number: "); b sc.nextInt () ; //Reading the second number System.out.println(b) System.out.println(a+" "+"+"+" "+b+" "+"="+" "+(a+b));//printing the arithmetic operati System.out.println(a+" "+"-"+" "+b+" "+"="+" "+(a-b));//printing the arithmetic operati System.out.println(a+" "+" *"+" "+b+" ""="+" "+(a*b)) ; //printing the arithmetic operati System.out.println(a+" "+"/"+" "+b+" "+"="+" "+(a/b));//printing the arithmetic operati System.out.println(a+" "+"%"+" "+b+" "+"="+" "+(a%b);//printing the arithmetic operati 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 Execute Mode, Version, Inputs & Arguments JDK 10.0.1 Interactive CommandLine Arguments

CommandLine Arguments Stdin Inputs -4 3 Execute Result CPU Time: 0.26 sec(s), Memory: 33496 kilobyte(s) compiled and executed in 1.093 sec(s) Enter the First number: -4 Enter the Second number: 3 -4 +3 = -1 -4 - 3 = -7 -4 * 3 = -12 -4 / 3= -1 -4 % 3= -1

CommandLine Arguments Stdin Inputs 12 3 Execute Result CPU Time: 0.27 sec(s), Memory: 33448 kilobyte(s) compiled and executed in 1.069 sec(s) Enter the First number: 12 Enter the Second number: 3 12 + 3= 15 12 - 3 9 12 *3 = 36 12 / 3= 4 12 % 3 =0


Related Solutions

Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problem : The Fraction class (Filename: TestFraction.java) Design...
An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from...
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from the command line: $ java CmdLineCalc 1 + 2 3.0 $ java CmdLineCalc 1 - 2.5 -1.5 $ java CmdLineCalc 3 + 4 - 5 2.0 $ java CmdLineCalc 6.5 - 7 + 8 7.5 To keep it simple, your program only needs to support addition (+) and subtraction (-). You may assume that, starting with the first argument, every other argument will be...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is...
Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is as follows: ______________________________________________________________________________ #include // FILL IN DIRECTIVE FOR FILES #include <iostream> #include <iomanip> using namespace std; // This program reads records from a file. The file contains the // following: student's name, two test grades and final exam grade. // It then prints this information to the screen. const int NAMESIZE = 15; const int MAXRECORDS = 50; struct Grades // declares a...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT