I am new to programming and in a course to learn.
In this assignment have to make a class, Person(String name, int year). Then make a bubble sort, that sorts the Person objects by year. And I need to test it by writing the names in the console sorted by year.
I am soooo stuck and the same name is written in the console 5 times.
public class Person {
private static String name;
private static int year;
public Person(String name, int year) {
super();
Person.name = name;
Person.year = year;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Person.name = name;
}
public static int getYear() {
return year;
}
public static void setYear(int year) {
Person.year = year;
}
public static void bubbleSort(Person[] list) {
boolean undone;
do {
undone = false;
for (int i = 0; i < list.length - 1; i++) {
for (int j = 0; j < list.length - 1; j++) {
if (list[j].getYear() > list[j + 1].getYear()) {
list[j] = list[j + 1];
list[j + 1] = list[j];
undone = true;
}
}
}
} while (undone);
}
public static void printArray(Person[] myPerson) {
int n = myPerson.length;
for (int i = 0; i < n; ++i)
System.out.println(myPerson[i].getName() + " ");
}
}
public class MainPerson {
public static void main(String[] args) {
Person[] myPerson = new Person[] { new Person("Susanne Hansen", 1999), new Person("Hans Jespersen", 1896),
new Person("Eva Gylden", 2789), new Person("Anders Aage", 1786), new Person("Ane Pulsar", 1345) };
Person.bubbleSort(myPerson);
Person.printArray(myPerson);
}
}
Here is the updated Code which generates output as you require:
class Person {
private String name;
private int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
public class Main {
public static void bubbleSort(Person[] list) {
boolean undone;
do {
undone = false;
for (int i = 0; i < list.length - 1; i++) {
for (int j = 0; j < list.length - 1; j++) {
if (list[j].getYear() > list[j + 1].getYear()) {
Person temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
undone = true;
}
}
}
} while (undone);
}
public static void printArray(Person[] myPerson) {
int n = myPerson.length;
for (int i = 0; i < n; ++i)
System.out.println(myPerson[i].getName() + " ");
}
public static void main(String[] args) {
Person[] myPerson = new Person[] { new Person("Susanne Hansen", 1999), new Person("Hans Jespersen", 1896),
new Person("Eva Gylden", 2789), new Person("Anders Aage", 1786), new Person("Ane Pulsar", 1345) };
bubbleSort(myPerson);
printArray(myPerson);
}
}
There are few mistakes in your code:
Your Code is:
if (list[j].getYear() > list[j + 1].getYear()) {
list[j] = list[j + 1];
list[j + 1] = list[j];
undone = true;
}
Need to update it to:
if (list[j].getYear() > list[j + 1].getYear()) {
Person temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
undone = true;
}
What was happening in your code was as you didn't store the value in a temporary variable the object at list[j] was getting lost.
static
in POJO/Data classes.
FYI POJO is a class that contains only data and functions to manipulate the data.Using static
will make java create the Object only once in memory and they will live throughout the lifecycle of the program.
As you have used Person.name
or Person.year
it will create problem, always use this this.name
/this.year
keyword to reference objects that are inside the same class.
You need to look into details of the Static Keyword,this Keyword and POJO Classes.