Question

In: Computer Science

This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...

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:

  1. On line 11, change the String myName from “your full name goes here!!!” to your first and last name. After this change, when you compile and run the program you should see your name output in the console.
  2. Following the example for addition on line 23, insert lines to calculate and output:
    1. the difference (subtraction)
    2. the product (multiplication)
    3. the quotient (division)
    4. the remainder (modulo)
  3. Add a line that outputs the message “that’s all folks!” before your program exits.

also please answer the following question

  1. Run your program and enter 3 for the first number and 0 for the second number. What happens?
  2. Run your program and enter 3.3 for the first number. What happens?
  3. Run your program and enter CAT for the first number. What happens?
  4. Uncomment the last System.out.println. (Line 28 in the original code posted to Canvas). Is the output the same as line 23?

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

Solutions

Expert Solution

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.


Related Solutions

How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Your task for this assignment is use the Java Canvas and Graphics classes to create an example of a computer generated image.
Your task for this assignment is use the Java Canvas and Graphics classes to create an example of a computer generated image. This is an opportunity for you to explore computer graphics and exercise some individual creativity. You should submit well-documented code, and once your program is done, create a presentation of your program. The presentation can be a PowerPoint or Word document, or something created with similar software. It could be a PDF file. Tell us what your prject is and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT