It is a simple add method with only 5 or 6 lines of code. The following are hint
ID: 3557626 • Letter: I
Question
It is a simple add method with only 5 or 6 lines of code.
The following are hints given:
It should have an if statement. This add method determines what number greater than or less than the other number, than it adds the positive or negative. I think this should be a private method.
The code should pattern after b1=b2+b3, ex: b1= b2add(b3) (not b1=add(b2,b3).
It should add arrays and array lists.
(It has been said that this add method doesn't technically add at least until the end but focuses on the signs.(pos or neg))
Also I would like it to accept the numbers from the keyboard, not have them assigned in there.
Explanation / Answer
import java.util.Scanner;
public class Addition {
static Scanner in = new Scanner(System.in);
int num;
public static void main(String[] args) {
Addition b1=new Addition();
Addition b3=new Addition();
Addition b2=new Addition();
System.out.println("Enter first number");
int num1=Integer.parseInt(in.nextLine());
b2.num=num1;
System.out.println("Enter first number");
int num2=Integer.parseInt(in.nextLine());
b3.num=num2;
b1=b2.add(b3);
if(b1.num>0)
System.out.println("Sum : +"+b1.num);
else
System.out.println("Sum : "+b1.num);
}
public Addition add(Addition a)
{
Addition temp=new Addition();
temp.num=this.num+a.num;
return temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.