Help with Java program

S

Stefan Ram

DeAndrea Monroe said:
I am trying to compile a java program that simulates a bank's
operation. I get the errors:
line 19, 46, 50,: '{' expected and ';' expected
line 28, 56, 76: missing return statement

Usually, when the bank simulation assignment is given,
students are expected to already know the more fundamental
topics of the

- syntax of expression statements, return statements
and blocks, and of the

- declaration of methods with and without return
statements.

When this knowledge is missing, the student should first
tackle more simple assignments to learn these fundamentals
in isolation and then later retry more advanced assignment
such as this bank simulation.
 
A

Alex Mentis

You have a lot more problems in this code than the compiler has told
you about so far. This looks like homework, so I won't do too much of
it for you, but here are some tips. Be advised, fixing the problems
you are asking about is not going to be enough, so I hope this isn't
due tomorrow.

Lines 19, 46, and 50 are causing compilation errors because you are
trying to pass parameters to a class, which you cannot do (there is
such a thing as a generic class that takes type parameters, but that's
an advanced topic that you don't need here). Instead of trying to pass
parameters to the Customer class, you should declare arrivalTime and
processTime as attributes of the class. For example:

public class Customer
{
private int arrivalTime;

...
}


You can give them their initial values with a constructor method when
you instantiate a new object of type Customer, or you could use a
mutator method for each of these variables to set their values after
creating the object with the default constructor. Right now you have
neither a constructor nor mutator methods, but you probably should.

Lines 28, 56, and 76 mean that methods which are declared to have
return values (for example, "public boolean done()" means the method
'done' should return a boolean value) must have a return statement in
their bodies (like you did with the arrival method).

Free hints:

1) your main method is empty
2) you can't instantiate a new customer inside the customer class itself
3) when you figure out where line 21 really should be, capitalization
will be important
4) in English, your 'done' method says "if the processTime is negative,
then make it more negative" - this is not what you want here

Stefan is right. You have some fundamentals to review. I recommend
you go in for office hours.

Good luck!
 
I

Ian Shef

I am trying to compile a java program that simulates a bank's
operation. I get the errors:
line 19, 46, 50,: '{' expected and ';' expected
line 28, 56, 76: missing return statement

my code can be found here: http://code.google.com/p/cschelp/source/checkout

I will help you, but next time, you should provide more help to the group
FIRST.

The URL that you provided does NOT take me directly to your code. I had to
discover the Browse button.
Having done that, I had to guess which of five files to review. I guessed
that you intended bank.java .

OK...

(1)
Every public class needs to go in a file of the same name. The SAME NAME,
even down to capitalization. Thus:

public class Bank should be in file Bank.java (NOT bank.java) .
public class Customer should be in file Customer.java .
public class Teller should be in file Teller.java .


(2)
There is one right way to define a class.
This is good:

public class Bank {
/* various class related stuff */
}

This is not:

public class Teller(){ }

The parentheses don't belong. The same for:

public class Customer(int arrivalTime, processTime)
{
/* more stuff */
}


(3)
Methods defined to return a result must have a return statement and must
return an appropriate result with that statement.

For example,
public boolean done()
{
if (processTime < 0)
{
--processTime;
}
}

is declared to return a boolean, but it doesn't.

(4)
Don't do:
import java.lang.*;

It is harmless, but unnecessary. It happens automatically.

(5)
This can't be good:

customer customer = new customer();

It should be:

Customer customer = new Customer();

Capitalization counts! Capitalization counts! Capitalization counts!

(6)
But why you would do the above, especially inside what was apparently
intended to be the Customer class, is beyond me.

(7)
This:

private int main(int numberOfTellers)

has the wrong signature to be a "main function".

(8)
There are more issues, like not defining variables and fields with proper
scope, and proper balancing of { ... } but it is too much to get into until
you get the other issues fixed.

Perhaps you need to read your textbook, meet with other students, meet with
a Teaching Assistant, or meet with your instructor. You are trying to do a
lot without understanding some very basic things about programming in Java.

Good Luck!
 
D

DeAndrea Monroe

http://code.google.com/p/cschelp/source/checkout



I will help you, but next time, you should provide more help to the group
FIRST.

The URL that you provided does NOT take me directly to your code.  I had to
discover the Browse button.
Having done that, I had to guess which of five files to review.  I guessed  
that you intended bank.java .

OK...


Thanks for your help. I made a few changes to the program, the changes
you suggested, I made before I read the post and I'm glad that I
actually did it right. But the code is located in the same space. I
still get errors after the changes have been made.
 
D

Donkey Hottie

I took a quick look at your code, and it makes no sense to me. Even if
you got it to compile, I don't think it would do anything useful. You
really need to step back and get some simpler programs working first.

Patricia

There are simpler, older, programs in the same folder. They are still
equally wrong, so it looks like the instructor does not give much help
for the assingments. :(

--

Swerve me? The path to my fixed purpose is laid with iron rails,
whereon my soul is grooved to run. Over unsounded gorges, through
the rifled hearts of mountains, under torrents' beds, unerringly I rush!
-- Captain Ahab, "Moby Dick"
 
D

DeAndrea Monroe

There are simpler, older, programs in the same folder. They are still
equally wrong, so it looks like the instructor does not give much help
for the assingments. :(

This is true. I've been trying to teach myself some of these steps and
the ways to do them correctly. I was just getting errors and I did a
some research and I can't find the answers, which is why I came to the
group for help.
 
A

Alex Mentis

DeAndrea said:
This is true. I've been trying to teach myself some of these steps and
the ways to do them correctly. I was just getting errors and I did a
some research and I can't find the answers, which is why I came to the
group for help.

Based on what I saw in your Bank program, I think a fundamental concept
you might start your review with is the idea of what a class is. A
class encapsulates (groups together) attributes (class variables) and
methods (functions) that operate on the data of the class.

In the design of your bank simulator, you have identified that you have
customers, and the information about customers that you want to keep
track of is an integer representing the time at which they get to the
bank and how long (loop iterations) they have to wait for service.
Each customer that arrives at the bank will have their own arrival time
and their own wait time. So these will be class variables, and each
new customer object you create will have their own copies with the
associated values.

Some methods that a Customer object needs are the ability to set and
get the data in the class variables. These functions are called
mutators and accessors, respectively. An example of a mutator you need
is a function to increase the wait time by one unit every time through
the loop. An example of an accessor you need is a function to return
the final waiting time at the end of the run.

To create a class that descibes this design, your code might look
something like this:

class Customer
{
private int arriveTime;
private int waitTime;

// mutator
public void wait()
{
waitTime++;
}

// accessor
public int getWaitTime()
{
return waitTime - arriveTime;
}
}

Note that this is not complete. You still need a way to get data into
the arriveTime class variable. Maybe you want to make a mutator like
the one that updates waitTime. Another way would be to write a class
constructor that takes a variable parameter indicating the loop
iteration you're on and stores that in the arriveTime variable (which
is the concept I use below).

To create a queue of customers, then, in your main function (which is
NOT inside the braces for the Customer class, because your main
function is going to create and use Customer objects) you would maybe
have:

for (int customer = 0; customer < numCustomers; customer++)
{
int randTime = ... // generate a random arrival time here
Customer aCustomer = new Customer(randTime);
// add aCustomer to the queue here
}

This will generate a queue of Customer objects, each one with its own
arrival time. Now use your loop and the Teller object to take
customers from the queue (adding the serviced customer's wait time to
an accumulator variable) and use the mutator on all the remaining
Customers in the queue to increment their wait times.

Etc.

One thing I've seen in your code is poor brace indentation discipline.
Using good indentation and lining up your block-enclosing braces will
help you see more easily what attributes and methods are in your
classes and what is not a member of it.

This information should point you in the right direction, at least.
 
M

markspace

This is true. I've been trying to teach myself some of these steps and
the ways to do them correctly. I was just getting errors and I did a
some research and I can't find the answers, which is why I came to the
group for help.


Here's some basic computer science info, if your instructor isn't
teaching. The classic computer program consists of three parts:

1. Input
2. Processing
3. Output

These steps run in that order. This worked for a long time, for punch
cards and green terminals, up until modern GUIs and MVC. However, many
small parts of a computer program, such as the controllers used in an
MVC design, can still be broken into those three parts, so the concept
is still useful.

Banking programs like yours can often be broken into these three parts.
The customer provides some input (their account number and a dollar
amount), you process it (withdrawal/deposit), then give the user his
result ($$, account report, no funds, etc.).


I had a friend in college who was pretty dyslexic. He would write is
programs as a kind of outline, to make sure he had the structure
correct, then fill in the "blanks" to produce output.

For example, your banking program might start like this:

public class Banking2 {
public static void main( String... arg ) {
System.out.println( "Hello Banking World" );
}
}

Once you get that working, add a bit of outline for the steps above.

public class Banking2 {
public static void main( String... arg ) {
System.out.println( "Hello Banking World" );
input();
processing();
output();
}
private static void input() {
}
private static void processing() {
}
private static void output() {
}
}

Now, get that running, just as it is. It won't do anything different,
but you won't waste time with braces missing or other basic errors,
because the braces are easy to see.

Continue adding small steps until you have something working.

To test, you may have to add extra debugging (println's work well) so
you can verify that you've the work correctly. These println's will
help you keep the program working as you modify it, so don't remove
them. If you make a new class, add a toString() method so it will print
easily and correctly.

For the final program, once you are sure it's all working, don't remove
the println's, just comment them out, so you can add them back if you
decide you need to make more changes.


public class Banking2 {

static List<CustInput> queue = new List<CustInput>();

class CustInput {
int accountNum;
char withdrawalDeposit;
int amount; // in cents
@Override
public String toString() {
return "(account num:"+accountNum+
" with/dep:"+withdrawalDeposit+
" ammount:"+ammount+")";
}
}

public static void main( String... arg ) {
System.out.println( "Hello Banking World" );
input();
processing();
output();
}

private static void input() {
for( int i = 0; i < 5; i++ ) {
CustInput trans = new CustInput();
trans.accountNum = i;
trans.withdrawalDepsoit = 'D';
trans.amount = i * 100;
queue.add( trans );
}
// now print to make sure we did that right
System.out.println( "Queue after first input step:" );
System.out.println( queue );
}

private static void processing() {
}
private static void output() {
}
}

Now I did all that with out checking it, so it'll probably have some
typos. I'll let you work out where they might be. I'm pretty sure I
got at least the first program correct.
 
L

Lew

DeAndrea said:
This is true. I've been trying to teach myself some of these steps and
the ways to do them correctly. I was just getting errors and I did a
some research and I can't find the answers, which is why I came to the
group for help.

There's no reason to despair just because your instructor won't/can't help.
You can use free online resources to get started:

<http://download.oracle.com/javase/tutorial/index.html>
<https://www.ibm.com/developerworks/java/>
<http://home.earthlink.net/~patricia_shanahan/beginner.html>
<http://mindprod.com/jgloss/jgloss.html>
 
M

markspace

For my first programs in a language I'm learning, I often go in the
order output, input, processing.



For design, perhaps, but the order that the computer program preforms
the steps is always input-processing-output.
 
C

Chase Preuninger

I am trying to compile a java program that simulates a bank's
operation. I get the errors:
line 19, 46, 50,: '{' expected and ';' expected
line 28, 56, 76: missing return statement

my code can be found here:http://code.google.com/p/cschelp/source/checkout

It would help if I could see the code. Remember that you need to put
a semicolon at the end of each line of code in java or use brackets.
The error message means that you left one of these out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top