Question

In: Computer Science

public class Sum2 { // TODO - write your code below this comment. } Download the...

public class Sum2 {
    // TODO - write your code below this comment.
}

Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4:

Sum: 7

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code ,Steps to run the code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


Note: If the below code is missing indentation please refer code Images

Typed Code:

class Sum2
{
public static void main(String[] args)
{
//accessing args[0] and changing it into integer
//storing it into num1
int num1 = Integer.parseInt(args[0]);
//accessing args[1] and changing it into integer
//storing it into num2
int num2 = Integer.parseInt(args[1]);
//adding those two numbers
//storing it into sum
int sum = num1+num2;
//printing the sum
System.out.print("sum:"+sum);
}
}
//code ended here

Steps:

1.Take the code into an editor.

2.Save that java file in any location

3.Go to command prompt and move to that location in command prompt.

command to move to that location where the Sum2.java exists --> cd location.

Here location is where Sum2.java is placed in your computer.

4.compile the code using command --> javac Sum2.java

where Sum2.java is file name

5.Acessing the class and giving arguments using command --> java Sum2 3 4

Sum2 is a class name and 3,4 are args[0] and args[1]

6.then we will get the output

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

public class FirstChar { // TODO - write your code below this comment. } Download the...
public class FirstChar { // TODO - write your code below this comment. } Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line argument foo is shown below: First...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
You will develop code in the ​StickWaterFireGame​ class only. Your tasks are indicated by a TODO....
You will develop code in the ​StickWaterFireGame​ class only. Your tasks are indicated by a TODO. You will also find more instructions in the form of comments that detail more specifically what each part of the class is supposed to do, so make sure to read them. ​Do not delete or modify the method headers in the starter code! Here is an overview of the TODOs in the class. TODO 1: Declare the instance variables of the class. Instance variables...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT