import java.util.LinkedList; import java.util.Scanner; public class ArithmeticOp
ID: 3797044 • Letter: I
Question
import java.util.LinkedList;
import java.util.Scanner;
public class ArithmeticOperatorsTwo{
public LinkedList head;
public LinkedList next;
public static Scanner userChoice;
public static void main(String[]args){
System.out.println("Please choose FIVE numbers to add, subtract,");
System.out.println("multiply, or divide.");
Addition(result);
}
public static int result;
public static int Addition(int list){
userChoice = new Scanner(System.in);
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
aL.add(d);
aL.add(e);
result = a + b + c + d + e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result;
}
public String toString() {
String result = "";
LinkedList current = head;
while(current.getFirst() != null){
current = (LinkedList) current.getFirst();
}
return "List: " + result;
}
}
class Link{
//reference to first link in list or last link added to list
public Link firstLink;
void LinkList(){
firstLink = null;
}
public boolean isEmpty(){
return(firstLink == null);
}
public void insertFirstLink(int userChoice){
Link newLink = new Link(userChoice);
newLink.next = firstLink;
firstLink = newLink;
}
}
Long story short the overall goal of this code is going to be to implement a simple legal lisp expression for addition, substraction, multiplication, and division. RIght now I'm just trying to accomplish the addtion part. What I want to do is create a list in the list class I'm trying to create and then implement that list into my addition method of my ArithmeticOperatorsTwo class. Once it's in that class in method I want to push,& pop the needed numbers from a the scanner input in order to acomplish the expression. Basiacally I'm complelty stumped and just need a push in the right direction or something(not looking for somone to do it for me though) just need help. Please and thank you.
Explanation / Answer
What I tried understanding from the question is that you want to create a LinkedList in Link file and that you want to use in ArithmeticOperatorsTwo class. For adding the inputs provided by the user. Hope I understood your question, what you are telling. Also, tried replicating the same below.
import java.util.LinkedList;
import java.util.Scanner;
public class ArithmeticOperatorsTwo{
public LinkedList head;
public LinkedList next;
public static Scanner userChoice;
public static void main(String[]args){
System.out.println("Please choose FIVE numbers to add, subtract,");
System.out.println("multiply, or divide.");
Addition(result);
}
public static int result;
public static int Addition(int list){
userChoice = new Scanner(System.in);
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
Link link = new Link();
link.insertFirstLink(a);
link.insertFirstLink(b);
link.insertFirstLink(c);
link.insertFirstLink(d);
link.insertFirstLink(e);
LinkedList<Integer> aL = link.getFirstLink();
result = a + b + c + d + e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result;
}
public String toString() {
String result = "";
LinkedList current = head;
while(current.getFirst() != null){
current = (LinkedList) current.getFirst();
}
return "List: " + result;
}
}
class Link{
private LinkedList<Integer> firstLink;
public Link(){
firstLink = new LinkedList<Integer>();
}
public boolean isEmpty(){
return(firstLink == null);
}
public void insertFirstLink(int userChoice){
if(isEmpty())
firstLink = new LinkedList<Integer>();
firstLink.add(userChoice);
}
public LinkedList<Integer> getFirstLink() {
return firstLink;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.