-4

Here's the code (number operations class is the second listed):

public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  // declare and instantiate ArrayList with generic type <NumberOperations>
  ArrayList<NumberOperations> numOpsList 
      = new ArrayList<NumberOperations>();

  // prompt user for set of numbers
  System.out.println("Enter a list of positive integers separated "
                    + "with a space followed by 0:");

  // get first user input using in.nextInt()
  int number = in.nextInt();

  // add a while loop as described below: 
// while the input is not equal to 0 
     // add a new NumberOperations object to numOpsList based on user 
  input
     // get the next user input using in.nextInt()

  while (number != 0) {
     numOpsList.add(new NumberOperations(number)); 
     number = in.nextInt();
  }

  int index = 0;
  while (index < numOpsList.size()) {
     NumberOperations num = numOpsList.get(index);
     System.out.println("For: " + num);
    // add print statement for odds under num
    // add print statement for powers of 2 under num

     index++;
  }


public class NumberOperations {

  // instance variables
  private int number;

  // constructor 

     /**
    * @param numberIn is number
    */
public NumberOperations (int numberIn) {
  number = numberIn;
}

 // methods

   /**
    * @return value
    */
public int getValue() 
{
  return number;
}

public String oddsUnder()
{
  String output = "";
  int i = 0;
  while (i < number) {
     if(i % 2 != 0) {
        output += i + "\t";
     }
     i++;
  }
  return output;
}
public String powersTwoUnder()
{
  String output = "";
  int powers = 1;
  while (powers < number) {
     output += powers + "\t";
     powers = powers * 2;
  }
  return output;
}
public int isGreater (int compareNumber)
{
  if (number > compareNumber) 
  {
     return 1;
  } 
  else if (number < compareNumber)
  {
     return -1;
  }
  else
  {
     return 0;
   }
}
public String toString()
{
   String output = "";
  return number + "";
}
}

The error I'm getting is that the compiler can't find "NumberOperations" anywhere. Its probably a very rudimentary issue I have, but I'm lost.

Edit: I added the class for numberoperations in case it helps. I thought I did everything right as far as this goes though.

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

0
Enter a list of positive integers separated with a space followed by 0:
1 2 3 4 0
For: 1
For: 2
For: 3
For: 4

I decorated the main method with a class

import java.util.*;

public class NumberOperationsTest {
  • yes, import was left to the helping User, too.

Closed the class just before the class NumberOperations, which I freed from the public declaration, to have it compilable in a single file.

In a different file, the public keyword is fine.

On compilation, you have to tell the classpath, to look in the current directory:

javac -cp . NumberOperationsTest.java
user unknown
  • 35,537
  • 11
  • 75
  • 121