0

The goal of the project is to create a calculator which will let the user choose which operation to use, which will then be solved and added to a linked list. The last option is to see the history which are past answers or solutions.

I'm quite new and this is a project I'm really trying to finish on my own but I have researched and still can't seem to quite understand what or how to fix. Please explain to me so that I can understand better! Also please comment on the design or flow of my code. Thanks!

import java.util.Scanner;
import java.util.LinkedList;

public class projectOne  {
    public static void main(String[] args) {
        int one = 0;
        int two = 0;
        int ans = 0;
        int selection = 0;

        Scanner scanner = new Scanner(System.in);
        LinkedList l = new LinkedList();

        do{
            System.out.println("\nSelect an option: \n1. Addition \n2. Subtraction \n3. Multiplication \n4.Division \n5. See History");
            selection = scanner.nextInt();

      /*      if (selection >= 1 && selection <= 4) {
                System.out.println("\nEnter two numbers to be used in relevant calculation: ");
                one = scanner.nextDouble();
                two = scanner.nextDouble();
            }
      */
            switch(selection) {
                case 1:
                    System.out.println("Performing Addition. \nEnter first number: ");
                    one = scanner.nextInt();
                    System.out.println("Enter second number: ");
                    two = scanner.nextInt();
                    System.out.println(one + " + " + two + " = " + (one + two));
                    ans = one + two;
                    l.append(ans);
                    break;
                case 2:
                    System.out.println("Performing Subtraction. \nEnter first number: ");
                    one = scanner.nextInt();
                    System.out.println("Enter second number: ");
                    two = scanner.nextInt();
                    System.out.println(one + " - " + two + " = " + (one - two));
                    ans = one - two;
                    l.append(ans);
                    break;
                case 3:
                    System.out.println("Performing Multiplication. \nEnter first number: ");
                    one = scanner.nextInt();
                    System.out.println("Enter second number: ");
                    two = scanner.nextInt();
                    System.out.println(one + " * " + two + " = " + (one * two));
                    ans = one * two;
                    l.append(ans);
                    break;
                case 4:
                    System.out.println("Performing Division. \nEnter first number: ");
                    one = scanner.nextInt();
                    System.out.println("Enter second number: ");
                    two = scanner.nextInt();
                    System.out.println(one + " / " + two + " = " + (one / two));
                    ans = one / two;
                    l.append(ans);
                    break;
                case 5:
                    System.out.println("History: \n" + l);
                    break;
            }
        } while(selection != 6);
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
karina
  • 41
  • 7
  • 2
    There is no such method. Did you mean `add(..)` instead? https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html – jmj Mar 03 '19 at 04:44
  • Try `add()` instead of `append()` – dave Mar 03 '19 at 04:44
  • try to use some editor like eclipse to remove these kind of errors. – Dinesh Mar 03 '19 at 04:51
  • There is actually another class LinkedList which the professor gave for us to use and has an append method public void append(Node node) { //if head is empty then head is equal to node if (empty()) { head = node; } ... I'm just very very new to coding so I really don't know how to exactly connect/relate(?) things – karina Mar 03 '19 at 04:52
  • 1
    If the available method is `void append(Node node)`, then you have to give it a `Node` as an argument, not an `int`. You need to 1) figure out how to "wrap" your `int` value (`ans`) in a new `Node` instance and 2) pass that `Node` instance to `append`. – Kevin Anderson Mar 03 '19 at 05:01
  • updated that code @karinaabad – Ryuzaki L Mar 03 '19 at 05:01
  • @karinaabad If you want use LinkedList which the professor gave for you, you do not `import the java.util.LinkedList`, you must `import your.professor.LinkedList` instead! – rockfarkas Mar 03 '19 at 06:56

1 Answers1

2

LinkedList does not have method append, you can use add, addLast.

Also, please avoid using raw type, use LinkedList<Integer> instead.

xingbin
  • 27,410
  • 9
  • 53
  • 103