Hi everyone, I am taking a java course and this is my second project for that class. For this project we are supposed to create 2 class files and a main project file. A Room class, a RoomSorting class and Project2 main file. This program takes a text file with classroom information and creates a array of objects and then sorts it by room capacity and then prints out to a JFrame with two text areas, one shows the unsorted list and the other shows the sorted list. The text file format is
building name|room number| room capacity. I have tested the Room class file with test programs from my professor and it works fine, but I am having trouble with the RoomSorting class file. I have not yet tested the sorting since I can't get the compareTo method to work. I'll appreciate any and all suggestions on how to fix this error. The error i get is :
RoomSorting.java:21: non-static method compareTo(Room) cannot be referenced from a static context
if (Room.compareTo(room) < 0 ) {
^
1 error
I have checked over the web on many different sites and came across others saying you have to instatiate an instance of that class but only if it the constructor has no arguments but the assignment I have has to have arguments as it was stated by the professor. I have tried many other ways but didn't work so i figured I would post and ask for help as this is the last part for me to finish my assignment.
These are the parts of the java files.
CODE
//Room.java
import javax.swing.*;
import java.util.*;
public class Room {
private String building, roomNumber;
private int capacity;
public Room (String building, String roomNumber, int capacity) {
this.building = building;
this.roomNumber = roomNumber;
this.capacity = capacity;
} // Room
public int compareTo (Room other) {
if (capacity < other.capacity)
return(-1);
else if (capacity == other.capacity)
return(0);
else
return(1);
} // compareTo
} // class Room
CODE
//Project2.java
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Project2 extends JFrame {
final static int MAX_ROOMS = 100;
static int arrayLength;
static Room [] roomsArray = new Room [MAX_ROOMS];
private JTextArea textAreaOriginal;
private JTextArea textAreaSorted;
private JTextField messageField;
public Project2() {
setSize (450, 300);
setLocation (100, 100);
setTitle ("Sorts file of rooms by capacity");
Container contentPane = getContentPane();
JPanel panel = new JPanel();
panel.setLayout (new GridLayout (1, 2));
contentPane.add(panel, BorderLayout.CENTER);
textAreaOriginal = new JTextArea();
textAreaOriginal.setEditable(false);
panel.add(new JScrollPane(textAreaOriginal));
textAreaSorted = new JTextArea();
textAreaSorted.setEditable(false);
panel.add(new JScrollPane(textAreaSorted));
messageField = new JTextField();
messageField.setEditable(false);
contentPane.add(messageField, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} // Project2 window
public static void main (String args[]) {
if (args.length !=1) {
System.out.println ("\nYou must specify a filename!");
System.exit(0);
} // if
final String inputFileName = args[0];
arrayLength = readFile (inputFileName, roomsArray);
Project2 window = new Project2();
// RoomSorting.sortByCapacity(roomsArray,arrayLength);
} // main method
public static int readFile (String filename, Room[] rooms) {
.....................
} // readFile
public static void displayResults (JTextArea area, Room[] rooms, int length) {
..............................
} // displayResults
} // Project2
CODE
//RoomSorting.java
import javax.swing.*;
import java.util.*;
public class RoomSorting {
public void sortByCapacity (Room[] rooms, int length) {
for (int lengthFilled = 0; lengthFilled < length -1; lengthFilled++) {
Room room = rooms[lengthFilled];
int indexLow = lengthFilled;
for (int j = lengthFilled +1; j <= length-1; j++) {
if (Room.compareTo(room) < 0 ) {
indexLow = j;
} // if
} // for
Room temp1 = rooms[indexLow];
rooms[indexLow] = rooms[lengthFilled];
rooms[lengthFilled] = temp1;
} // for
} // method sortByCapacity
} // class RoomSorting
I am sorry for posting all this code but I figured it would be easier for someone looking at it to understand the purpose of the program.
Thanks,
HLC
This post has been edited by HLC11: 19 Mar, 2008 - 10:55 PM