1

I am trying to sort an ArrayList in increasing order in reference to a certain variable. This is the problem question.

q5: Create a public class named Snow with private instance variables vast, prior, ethnic, and remarkable each of type int. You may add any other methods and variables you'd like to this class.

Outside of Snow (in the Problem Set class) write a public static method named sortSnow that takes an ArrayList of Snows as a parameter and returns void. This method will sort the input by the variable remarkable in increasing order

This is what I wrote.

public class snow implements Comparable<snow> {
    private int vast;
    private int prior;
    private int ethnic;
    private int remarkable;

    public snow( int vast , int prior, int ethnic ,int remarkable) {
        this.vast=vast;
        this.prior = prior;
        this.ethnic = ethnic;
        this.remarkable = remarkable;
    }

    public int getEthnic() {
        return ethnic;
    }

    public void setEthnic(int ethnic) {
        this.ethnic = ethnic;
    }

    public int getPrior() {
        return prior;
    }

    public void setPrior(int prior) {
        this.prior = prior;
    }

    public int getVast() {
        return vast;
    }

    public void setVast(int vast) {
        this.vast = vast;
    }

    public int getRemarkable() {
        return remarkable;
    }

    public void setRemarkable(int remarkable) {
        this.remarkable = remarkable;
    }

    public int compareTo(snow compareSnow) {
        // TODO Auto-generated method stub
        int compareThese = ((snow) compareSnow).getRemarkable(); 

        //ascending order
        return this.remarkable - compareThese;
    }

    }
    public static void sortSnow(ArrayList<snow>input){
         Collections.sort(input);  
    }

I am not understanding what the error means. The autolab is giving me this error:

Could not find class submission.ProblemSet$Snow

AxelH
  • 14,325
  • 2
  • 25
  • 55
jhonathan
  • 21
  • 2
  • Can you tell us what is the problem ? See how to provide a [mcve] and [edit] your question. And Welcome ! – AxelH May 03 '18 at 05:45
  • 2
    You are supposed to write a `Snow` class nested inside the `ProblemSet` class. Not a `snow` class. – Eran May 03 '18 at 05:45
  • I'm not exactly sure what your question is. Use `List.sort` or `Collections.sort` with an appropriate `Comparator` – sprinter May 03 '18 at 05:45
  • when i submit my problem it says its wrong and Could not find class submission.ProblemSet$Snow. I dont understand where Im wrong. and what does the error mean? – jhonathan May 03 '18 at 05:46
  • How, be careful on the formatting next time. I didn't catch most of the description. [Burkhard's answer](https://stackoverflow.com/a/50147784/4391450) will help you. But you might need to check the full hierarchy of your class. "_Could not find class submission.ProblemSet$Snow_" means that the `Snow` should be an inner class of `ProblemSet`. – AxelH May 03 '18 at 05:52
  • @AxelH I'm fine to reopen when the question is corrected. – lexicore May 03 '18 at 06:05
  • @AxelH I've corrected the title. But now I can't close it again (as it should be). :) – lexicore May 03 '18 at 06:10
  • Me neither @lexicore ... but at least, justice have been done ;) I know we could have let it closed but an incorrect flag on rookie can be frustrating for them. – AxelH May 03 '18 at 06:16
  • @AxelH I totally agree with you. The corrected question (and answers) are now more useful for future readers. I regret I was mistaken with the duplicate. – lexicore May 03 '18 at 06:56

2 Answers2

1

Java is case sensitive i.e. snow is not Snow is not sNoW. Rename your class to Snow and try again. Also, it is ArrayList and not arraylist.

Then to sort a List, you can use Collections.sort.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
0

I think this is you want to achieve

Save below code in file called "Snow.java" compile it and try to run it.

import java.util.ArrayList;
import java.util.Collections;

//As ".java" file can contain only single public java class
//I made Problem set class non-public so we can use its main method
//to run and see output

class ProblemSet {

public static void main(String[] args) {

    Snow one = new Snow(1,1,1,1);
    Snow two = new Snow(1,1,1,2);
    Snow three = new Snow(1,1,1,3);
    Snow four = new Snow(1,1,1,4);
    Snow five = new Snow(1,1,1,5);
    Snow six = new Snow(1,1,1,6);

    ArrayList arrayList = new ArrayList();
    arrayList.add(one);
    arrayList.add(three);
    arrayList.add(five);
    arrayList.add(two);
    arrayList.add(six);
    arrayList.add(four);

    System.out.println("Without sort");
    System.out.println(arrayList);

    sortSnow(arrayList);

    System.out.println("With sort");
    System.out.println(arrayList);

}

//this is your static method which takes argument as array list of Snow
//And it applies sorting logic based on compareTo method which you wrote
//in Snow class. As per java best practice Class name should start with 
//Upper case letters and follow camel casing I renamed your class from 
//"snow" to "Snow"
public static void sortSnow(ArrayList<Snow> input){
    Collections.sort(input);
}

}

//This is you public class Snow
//If you want to keep it in separate java file put it

public class Snow implements Comparable<Snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;

public Snow(int vast, int prior, int ethnic, int remarkable) {
    this.vast = vast;
    this.prior = prior;
    this.ethnic = ethnic;
    this.remarkable = remarkable;
}

public int getEthnic() {
    return ethnic;
}

public void setEthnic(int ethnic) {
    this.ethnic = ethnic;
}

public int getPrior() {
    return prior;
}

public void setPrior(int prior) {
    this.prior = prior;
}

public int getVast() {
    return vast;
}

public void setVast(int vast) {
    this.vast = vast;
}

public int getRemarkable() {
    return remarkable;
}

public void setRemarkable(int remarkable) {
    this.remarkable = remarkable;
}

public int compareTo(Snow compareSnow) {
    // TODO Auto-generated method stub


    int compareThese = ((Snow) compareSnow).getRemarkable();

    //ascending order
    return this.remarkable - compareThese;

}

//This is added because when you use array list to print
//it will print remarkable of particular Snow object
@Override
public String toString() {
    return String.valueOf(remarkable);
}
}
Abhishek
  • 3,348
  • 3
  • 15
  • 34
  • Please explain what you are doing instead of posting a full code on an answer that only required a few updates. – AxelH May 03 '18 at 06:16