# Home assignment

• November 14th, 2012, 07:49 AM
yogafisken
Home assignment
Hi,

I am a very beginner in java coding and would need help on getting the codes for these exercises. Why I'm asking is because time is running out and deadline coming up too fast.

1. Small mathematics application. Write a program that asks the user mathematical questions (at
least 5 questions) such as “What is 5*21?”. The user should be able to stop the program by writing 0
or by answering correctly and thus getting the next question. (Hint you can use for example .equals()
if you want to compare Strings).

2. Ticket vending application. Create a vending system for individual train ticket system. We assume
that there are only four possible stations called: Helsinki, Pasila, Tampere and Turku. The tracks of
trains are built in a way that all the trains need to go from Helsinki to Pasila. From Pasila they can go
either to Turku or Tampere. We assume that you have to travel from Tampere to Turku via Pasila.
Our system only sells one way tickets, which have fixed prices for the different possible journeys. All
the combinations of one departing and one destination stations are possible.
Build an application which asks the user his name and then asks the journey he wants to undertake.
Then print to the screen the journey and the cost of the ticket (hint: you can invent the price table
for the different tickets, but they should be somehow logical, or current, prices).

3. Restaurant reservation management system. The restaurant has 5 tables and 18 possible seats
(tables of 6, 4, 4, 2, 2). We assume that one table can only be reserved once for one evening and
that the seating cannot be changed.
Make a management system of a restaurant clerk, which stores the availability of tables. The clerk
taking the reservations is the user. In the beginning of the evening, there are no reservations.
Reservations come one at a time. Reservations can be for any number of people, but the seating
cannot be changed.
Reservations can only be accepted if there is room for them and otherwise the clerk should be
notified that there is room.
To minimize the empty tables, system should place the customers to the smallest possible table, if
there is room. For example a reservation of 3 must be placed to the table of 4, if there is still room.
Otherwise, it will need to be placed on the table of 6. If both are full, the reservation cannot be
accepted and the user needs to be notified. Store the reservation information to the system and
print the reservation situation out, if clerk wants to see the reservations, if all the tables are full or if
the clerk wants to stop accepting new reservations.
• November 14th, 2012, 07:55 AM
Purple01
Re: Home assignment

Now it looks like you want us to do your homework....
And that is not where this forum is for.
On the other hand, we do help people who are stuck with there code. :-)
• November 15th, 2012, 09:02 AM
yogafisken
Re: Home assignment
/**

*
* This is a train ticket vending program for a train system with 4 stations.
* The system only sells one way tickets.
*/
import java.util.*;
public class TicketApp
{
public static int HP = 3, PTa = 31, PTu = 31; //Defining the variables
public static String from, to, name;

public static void main (String[] args)
{
name = reader.nextLine(); //The user's name

System.out.println("From where are you travelling:"); //Sytem output
System.out.println("Helsinki, Pasila, Tampere or Turku?");
from = reader.nextLine(); //The city of departure

while (! from.equals("Helsinki") && ! from.equals("Pasila") && ! from.equals("Tampere") && ! from.equals("Turku")) //Checking the validity of the input
{
System.out.println("Invalid location. Please choose between Helsinki, Pasila, Tampere and Turku");
}

System.out.println("What is your destination:"); //System output
System.out.println("Helsinki, Pasila, Tampere or Turku?");

while (! to.equals("Helsinki") && ! to.equals("Pasila") && ! to.equals("Tampere") && ! to.equals("Turku")) //Checking the validity of the input
{
System.out.println("Please enter a valid destination city (Helsinki/Pasila/Tampere/Turku)");
}

if (from.equals(to)) //Making sure that the city of departure and the destination are not the same
{
System.out.println("The city of departure and the destination are the same. You do not need a ticket.");
}

//This is the switch for the different routes
switch(from+to)
{
case "HelsinkiPasila": HP();
break;
case "PasilaHelsinki": HP();
break;
case "HelsinkiTampere": HTa();
break;
case "TampereHelsinki": HTa();
break;
case "HelsinkiTurku": HTu();
break;
case "TurkuHelsinki": HTu();
break;
case "PasilaTampere": PTa();
break;
case "TamperePasila": PTa();
break;
case "PasilaTurku": PTu();
break;
case "TurkuPasila": PTu();
break;
case "TampereTurku": TaTu();
break;
case "TurkuTampere": TaTu();
break;
}
}

public static String HP() //Outcome if the user travels from Helsinki to Pasila
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+HP+" euros");
return "0";
}
public static String HTa() //Outcome if the user travels from Helsinki to Tampere, and so on
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+(HP+PTa)+" euros");
return "0";
}
public static String HTu()
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+(HP+PTu)+" euros");
return "0";
}
public static String PTa()
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+PTa+" euros");
return "0";
}
public static String PTu()
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+PTu+" euros");
return "0";
}
public static String TaTu()
{
System.out.println("Here is your journey:\nFrom: "+from+"\nTo: "+to+"\nPrice: "+(PTa+PTu)+" euros");
return "0";
}
}

I cant get the switch working; it says that: incompatible type; found java.lang.String but expected int. What should i do?
• November 15th, 2012, 03:13 PM
jps
Re: Home assignment
Quote:

I cant get the switch working; it says that: incompatible type; found java.lang.String but expected int. What should i do?
Switch statements do not work with strings in all versions. You are using an older version where switch only supports int. Upgrade or use int instead of String.