I have been working on a program with these instructions:
Station Information Write a program that gives information about underground stations. The user should first input how many stations they wish to ask about and then be allowed to name that many stations. The program should say whether they have step free access and the distance that must be travelled to get from entrance to platform. A new type called Station must be created (a record type) and each separate piece of information about a station should be stored in a separate field of the record (its name - a String, whether they have step-free access - a boolean, and the distance in metres - an integer).
A separate method must be written that given a String (a station name) as argument returns a String containing the correct information about the station. The String should be printed by the calling method. An example run of the program (bold words are typed by the user):
How many stations do you need to know about? 4
What station do you need to know about? Stepney Green
Stepney Green does not have step free access.
It is 100m from entrance to platform.
What station do you need to know about? Kings Cross
Kings Cross does have step free access.
It is 700m from entrance to platform.
What station do you need to know about? Oxford Street
Oxford Street is not a London Underground Station.
What station do you need to know about? Oxford Circus
Oxford Circus does have step free access.
It is 200m from entrance to platform.
This is my program:
//this program tells the user whether a station is a step free access station or not and how far is it from the platform
import java.util.Scanner; // imports the scanner function to input data from the user
class StationInformation
{
public static void main(String [] args) //main method where methods are sequenced
{
int numberOfStations = inputint("how many stations do you want to know about?");
String station;
for(int i=1; i<=numberOfStations; i++)
{
station = inputstring("what station do you want to know about?");
search_station(station);
}
System.exit(0);
}
// A method to input integers
public static int inputint (String message)
{
Scanner scanner = new Scanner(System.in);
int answer;
System.out.println(message);
answer = Integer.parseInt(scanner.nextLine());
return answer;
} // END inputInt
public static String inputstring(String message)
{
Scanner scanner = new Scanner(System.in);
String answer;
System.out.println(message);
answer = scanner.nextLine();
return answer;
}
public static String create_message(Station station)
{
String message;
if (station.step_free_access == true)
{
message = (station.name + "does have step free access. " + "it is "+ station.distance_from_platform +"m away from the entrance");
}
else
{
message = (station.name + "does not have step free access. " + "it is "+ station.distance_from_platform +"m away from the entrance");
}
return message;
}
public static void create_messages(String message1,String message2, String message3 , String message4)
{
Station mile_end = new Station();
Station oxford_circus = new Station();
Station kings_cross = new Station();
Station stepney_green = new Station();
mile_end.distance_from_platform = 50;
mile_end.name = "Mile End ";
mile_end.step_free_access =false;
message1 = create_message (mile_end);
oxford_circus.distance_from_platform = 200;
oxford_circus.name = " Oxford Circus ";
oxford_circus.step_free_access = true;
message2 = create_message(oxford_circus);
kings_cross.distance_from_platform = 700;
kings_cross.name = " kings cross ";
kings_cross.step_free_access = true;
message3 = create_message(kings_cross);
stepney_green.distance_from_platform = 300;
stepney_green.name = " Stepney Green ";
stepney_green.step_free_access = false;
message4 = create_message(stepney_green);
return ;
}
public static String search_station(String station)
{
String mileEndMessage;
String oxfordCircusMessage;
String kingsCrossMessage;
String stepneyGreenMessage;
create_messages(mileEndMessage , oxfordCircusMessage , kingsCrossMessage , stepneyGreenMessage);
if(station.equals("Mile End") )
{
System.out.println(mileEndMessage);
}
else if (station.equals("kings cross") )
{
System.out.println(kingsCrossMessage);
}
else if (station.equals("oxford circus") )
{
System.out.println(oxfordCircusMessage);
}
else if (station.equals("stepney green") )
{
System.out.println(stepneyGreenMessage);
}
else
{
System.out.println(station + " is not a London underground station ");
}
}
class Station //a record to store information about stations
{
int distance_from_platform;
String name;
boolean step_free_access;
}
}
I keep getting these errors:
StationInformation.java:65: error: non-static variable this cannot be referenced from a static context
Station mile_end = new Station();
^
StationInformation.java:66: error: non-static variable this cannot be referenced from a static context
Station oxford_circus = new Station();
^
StationInformation.java:67: error: non-static variable this cannot be referenced from a static context
Station kings_cross = new Station();
^
StationInformation.java:68: error: non-static variable this cannot be referenced from a static context
Station stepney_green = new Station();
^
4 errors
I want to fix the program and make it cleaner and more understandable.
Aucun commentaire:
Enregistrer un commentaire