Comparator in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to solve my homework. This is the code I use. Comparator for sorting a student list in 2 ways.
public class Student {
public static final Comparator<Student> BY_NAME = new ByName();
public static final Comparator<Student> BY_Gpa = new ByGpa();
private static class ByName implements Comparator<Student> {
public int compare(Student v, Student w) {
return v.getName().compareTo(w.getName());
}
}
private static class ByGpa implements Comparator<Student> {
public int compare(Student v, Student w) {
if (v.getGpa() == w.getGpa()) return 0;
else if (v.getGpa() < w.getGpa()) return -1;
else return 1;
}
}
}
I don't understand Collection.sort. Why does it have to use a complicated form like that? Why not just a static function in the class like C++ for sorting, instead of returning a class that implements Comparator which has a method compare? It's too complex.

Java 8's enhancements to the Comparator interface make it a lot more elegant:
public static final Comparator<Student> BY_NAME = Comparator.comparing(Student::getName);
public static final Comparator<Student> BY_Gpa = Comparator.comparingInt(Student::getGpa);

Related

How to create a private array and access it by the main method in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a class outside the public class but in the same file, and in that class I wanna create a private array which can only be accessed by creating object through the previous public class. I also wanna store data to that array through the public class.
I suggest you to start learning java, this will save you from asking this kind of question next time. For the moment you can find how to achieve what you are asking for in the example bellow :
public class Learning {
public static void main(String[] args) {
Course course = new Course();
List<String> materials = new ArrayList<>();
materials.add("java basic courses");
materials.add("OOP courses");
// here we use setters to set course materials and notes
course.setMaterials(materials);
course.setNotes(new int[] {19,20});
System.out.println("Display course materials");
for (String material : course.getMaterials()) {
System.out.println("Material : " + material);
}
System.out.println("Display Notes");
for (int note : course.getNotes()) {
System.out.println("Note : " + note);
}
}
}
class Course {
private List<String> materials;
private int[] notes;
public List<String> getMaterials() {
return materials;
}
public void setMaterials(List<String> materials) {
this.materials = materials;
}
public int[] getNotes() {
return notes;
}
public void setNotes(int[] notes) {
this.notes = notes;
}
}

Creating a class that has integers, getter/setters, with input validation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on this Java program where I am supposed to write a class called kumquat that has an integer age with getter and setters. Additionally, I need to do input validation. I can't figure out what I'm doing wrong. I'm sorry if this is really simple I'm just still really new to this.
public class Person
{
private int age;
public int getAge()
{
return age;
}
public void setAge(int newAge)
{
this.age = newAge;
}
}
and then my main
public class Kumquat
{
public static void main(int[] args)
{
Person myObj = new Person();
myObj.setAge("5");
System.out.println(myObj.getAge());
}
}
Everything is ok in the Person class. Although in your main the method Person::setAge receives an int as parameter and you're trying to pass a String in line myObj.setAge("5");. Try passing an int like myObj.setAge(5); instead.
you should write String instead of int and setAge should not be string
public static void main(String[] args)
{
Person myObj = new Person();
myObj.setAge(5);
System.out.println(myObj.getAge());
}

Why i am getting this output using java TreeSet Compare method? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I used a Comparator class to define the sorting of the StringBuffer.I have implemented the Comparator class and the Comparator method compare.
Why i am getting output like this?
Code:
import java.util.Comparator;
import java.util.TreeSet;
public class SortestSetDemo {
public static void main(String[] args) {
TreeSet t1 = new TreeSet(new MyComparator());
t1.add(new StringBuffer("a"));
// t1.add("d");
t1.add(new StringBuffer("q"));
t1.add(new StringBuffer("w"));
t1.add(new StringBuffer("r"));
System.out.println(t1);
}
}
class MyComparator implements Comparator {
public int compare(Object ob1, Object ob2) {
// String i1=(String)ob1;
String i1 = ob1.toString();
// String i2=(String)ob2;
String i2 = ob2.toString(); //corrected error here instead of ob1.toString it is ob2.toString()
return -i1.compareTo(i2);
}
}
Output Shown:
[a] instead of [a,q,r,w]
You have a typo in your code. String i2 = ob1.toString(); .
This should be
String i2 = ob2.toString();
Below code is working fine -
import java.util.Comparator;
import java.util.TreeSet;
public class SortestSetDemo {
public static void main(String[] args) {
TreeSet t1 = new TreeSet(new MyComparator());
t1.add(new StringBuffer("a"));
// t1.add("d");
t1.add(new StringBuffer("q"));
t1.add(new StringBuffer("w"));
t1.add(new StringBuffer("r"));
System.out.println(t1);
}
}
class MyComparator implements Comparator {
public int compare(Object ob1, Object ob2) {
// String i1=(String)ob1;
String i1 = ob1.toString();
// String i2=(String)ob2;
String i2 = ob2.toString();
return -i1.compareTo(i2);
}
}
Output:
[w, r, q, a]

ArrayList .add method missing [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello I have the following problem: I want to create an arraylist and want to add some items.
But somehow the .add Method is not there.
import java.util.ArrayList;
public class Chairing{
private int numbers;
ArrayList<Chairs>myList = new ArrayList<Chairs>();
myList.add(5,new Chairset("10"));
}
public class Chair{
int price;
String info;
public Chair(int price, Chairset c){
this.price = price;
info = c.getInfo();
}
}
public class Chairset{
String info;
public Chairset(String id){
id = info;
}
}
For some Reasons I can't add something in my new ArrayList. The constructor for Chair needs a price and an object Chairset. Chairset needs an id.
The problem is your classes have no common type, the tightest generic bound the list can have would be Object. Either use a marker interface, or notice the similarity between Chair and Chairset and have one extend the other - giving them a common type.
Also note that the line in your code where you add to the list is not in a legal location - it must be within a method.
Try this:
public class Chairing {
private int numbers;
List<Chairset> myList = new ArrayList<Chairset>();
public void someMethod() {
myList.add(5,new Chairset("10"));
}
}
public class Chair extends Chairset {
int price;
public Chair(int price, Chairset c){
super(c.getInfo());
this.price = price;
}
}
public class Chairset {
String info;
public Chairset(String id){
id = info;
}
}

How to make this method use ArrayList? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Course {
private String courseName;
ArrayList<String> students = new ArrayList<>();
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;//<-- Line 15
numberOfStudents++;
}
public ArrayList getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
}
Line 15 I am getting error "Array required, but ArrayList found.
I am unsure what to do here as I am new to strings and such.
students is declared as an ArrayList. This notation
students[numberOfStudents] = student;
only works for array types. You should use
students.add(student);
Please read the javadoc for ArrayList.
You also don't need to keep a field to hold the number of students, as
students.size();
will give you that.
If you want to use ArrrayList in your program:
public void addStudent(String student) {
students.add(student);
}
public int getNumberOfStudents() {
return students.length();
}
Also then you do not require numberOfStudents variable

Categories

Resources