In: Computer Science
This is a programming assignment!!!
Start with the Assignment3.java source code posted to Canvas.
This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0)
Make the following changes/additions to the code:
also please answer the following question
hERE'S THE JAVA CODE FOR ASSIGMET 3
import java.util.Scanner; public class Assignment3 { public static void main(String[] args) { // this Java code creates three variables (covered in lecture #4 slide 80) // int are counting numbers (1,2,3...) // String is a sequence of characters... int n1 = 0; int n2 = 0; String myName = "your full name goes here!!!"; // replace this String with your full name... System.out.println("my name is "+myName); // this Java code gets input from the user (covered in lecture #4 slide 53) Scanner fromUser = new Scanner(System.in); System.out.print("enter your first number..."); n1 = fromUser.nextInt(); System.out.print("enter your second number..."); n2 = fromUser.nextInt(); System.out.println("you entered "+n1+" and "+n2); fromUser.close(); // Your code goes below...(remember arithmetic operators are cover in lecture #4 slide 62) System.out.println("add: "+n1+" + "+n2+" = "+(n1+n2)); // addition provided as example... // question to think about... // uncomment the following line and look at the result...why is this different? what is going on here? // System.out.println("add??? "+n1+" + "+n2+" = "+n1+n2); } // endmain... } // endclass Assignment3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// this Java code creates three variables (covered in lecture #4
slide 80)
// int are counting numbers (1,2,3...)
// String is a sequence of characters...
int n1 = 0;
int n2 = 0;
String myName = "Supraja Goud"; // replace this String with your
full name...
System.out.println("my name is "+myName);
// this Java code gets input from the user (covered in lecture #4
slide 53)
Scanner fromUser = new Scanner(System.in);
System.out.print("enter your first number...");
n1 = fromUser.nextInt();
System.out.print("enter your second number...");
n2 = fromUser.nextInt();
System.out.println("you entered "+n1+" and "+n2);
fromUser.close();
// Your code goes below...(remember arithmetic operators are cover
in lecture #4 slide 62)
System.out.println("add: "+n1+" + "+n2+" = "+(n1+n2)); // addition
provided as example...
System.out.println("diffrence: "+n1+" - "+n2+" = "+(n1-n2)); //
subtraction
System.out.println("product: "+n1+" * "+n2+" = "+(n1*n2));
//multiplication
System.out.println("quotient: "+n1+" / "+n2+" = "+(n1/n2)); //
division
System.out.println("remainder: "+n1+" % "+n2+" = "+(n1%n2));
//modulo division
System.out.println("that's all folks!");
// question to think about...
// uncomment the following line and look at the result...why is
this different? what is going on here?
System.out.println("add??? "+n1+" + "+n2+" = "+n1+n2);
} // endmain...
} // endclass Assignment3
output obtained after execution for n1=2 and n2=4 :
---------------------------------------------------------------------
my name is Supraja Goud
enter your first number...2
enter your second number...4
you entered 2 and 4
add: 2 + 4 = 6
diffrence: 2 - 4 = -2
product: 2 * 4 = 8
quotient: 2 / 4 = 0
remainder: 2 % 4 = 2
that's all folks!
add??? 2 + 4 = 24
(i) for n1=3 and n2=0:
-----------------------------
my name is Supraja Goud
enter your first number...3
enter your second number...0
you entered 3 and 0
add: 3 + 0 = 3
diffrence: 3 - 0 = 3
product: 3 * 0 = 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:26)
here we get divide by zero error because we gave the 0 as input.
(ii) output for n1=3.3 :
-----------------
here we get the error InputMisMatchException because n1 is a integer variable but we give the floating point value as input.
my name is Supraja Goud
enter your first number...3.3
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:16)
(iii) output for n1=cat:
------------------------------
in this case also, we got InputMisMatchexception because n1 is integer but cat is string. see below
my name is Supraja Goud
enter your first number...cat
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:16)
(iv) in line 28 if we uncomment the line then the output obtained for add at line 28 is not same as add at line 23. it is because at line 28, it concatenates the two integers using +, if we use paranthesis() for n1 and n2 with + then it adds the n1 and n2 which happend at line 23.
check the below output:
my name is Supraja Goud
enter your first number...1
enter your second number...3
you entered 1 and 3
add: 1 + 3 = 4
diffrence: 1 - 3 = -2
product: 1 * 3 = 3
quotient: 1 / 3 = 0
remainder: 1 % 3 = 1
that's all folks!
add??? 1 + 3 = 13
first add means sum of n1 and n2
if we dont use paranthesis then it means concatenation like last line of output.