Coding question

I'm doing a project for class and I just have a question on how to code this particular problem. I realize that you guys hate it when people come in here looking for straight answers so any suggestions to get me to the answer will help. I'd prefer to figure it out for myself anyway.

For the project, I have to create a program that will read from a file or read user input mathematical expressions and decide whether they are valid or invalid expressions, count which is which, and solve the valid expressions. The part I'm hung up on is, how do I code the program so that it can read from a file or read from a user input based on what is input? Like I said, i'd prefer to figure it out for myself so any hints or suggestions would be greatly appreciated.

(I've only just started learning Java so I'm sorry if I seem like an idiot)

[858 byte] By [KHill054a] at [2007-10-2 23:36:43]
# 1
Reading from a file is probably the easiest part of the assignment. Getting user input is generally pretty easy as well. What trouble are you having exactly?
paulcwa at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 2
EDIT: Nevermind, I figured that part out. Thanks for the response though.Message was edited by: KHill054
KHill054a at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 3

So the user types in something, and the program has to determine whether what they typed in is supposed to be an expression, or a filename?

Generally, I'd say that you can't tell them apart, so you should do both. Whether doing both means rejecting the input twice (rejecting it both as an invalid expression and an invalid or missing file) or accepting it twice (accepting it as a filename and as an expression) depends on whether a filename is also a valid expression.

So for example: when a user inputs something, you'd check to see if it's a filename. If it is, see if the file exists. If it doesn't, interpret it as an expression. If the expression is invalid, give the error message "Bad expression or bad filename". Or you can do something similar if it's valid as both.

On the other hand, maybe there's a heuristic you can apply. For example, if the user types his input starting with "file:" then chop off the leading "file:" and use the rest as the filename, otherwise treat it as an expression to evaluate. Or maybe all filenames will be URLs generally (starting with a transfer type, like "file:" or "http:"), so you can use that to detect files.

By the way, consider this: if you apply a rule like that, then arguably "file:filename.txt" IS a valid expression -- it's an expression whose meaning is that the program should open a file and process it. So if you look at it that way, it's not that you're doing one or the other -- you'd always be evaluating expressions, it's just in some cases the expression is a directive to open a file. Interesting, eh?

paulcwa at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 4

This thing is going to be a monster for me. Just so you know what the problem is, I'll type up what it's asking. I'm NOT asking for the answers.

--

"The solution for this project will consist of a driver module (Proj1), a RationalNumber class, and a RationalExpressions interface.

The program processes a set of one-line expressions. Each expression is read into the program and processed. These expressions will be in infix notation and can be input to the program from the keyboard or via a disk file. The program output will be displayed to the monitor. Command line options are used to control the input source for the program.

If an input expression is malformed (not of the form operand operator operand) an error message is displayed and the program should continue. If the operator or operand(s) entered are invalid, an error message is displayed and the program should continue. With the logical operators, the program should display either TRUE or FALSE depending upon the truth-value of the expression. For the arithmetic operators, the calculated result should be displayed.

A concrete RationalNumber class should be created. This class must implement the RationalExpressions interface. A rational object should be created for each rational number read in as input and should be used for processing within the application. Selection logic is used to process the operands based on the operator value. An instance method must be used to implement the evaluation logic for each operator.

Before the program exits, a summary report should be displayed indicating the total number of both valid and invalid expressions encountered. The application should close any files used before exiting."

--

This is what i have so far:

import java.io.*;

class Proj1

{

//This is the driver class

public static void main(String [] args) throws Exception

{

//Message displayed at the start of the program

System.out.println("+++++++++++++++++++++++++++++++++++++++++");

System.out.println("+ Welcome to the expression connection +");

System.out.println("+++++++++++++++++++++++++++++++++++++++++");

System.out.println();

System.out.println();

BufferedReader br;

String buffer = null;

if (args.length != 0)

{

if (args.length != 2)

{

//Error message that displays if the user doesn't put in two parameters

System.out.println("Invalid number of parameters: Usage is - java [-f] [filename]");

System.exit(1);

}

if (!args[0].equals("-f"))

{

//Error message that displays if the first parameter is incorrect

System.out.println("Invalid first parameter: Usage is - java [-f] [filename]");

System.exit(2);

}

File input = new File(args[1].trim());

System.out.println(input.toString());

if (!input.exists())

{

//Error message that displays if the file name is incorrect

System.out.println("Invalid filename: Usage is - java [-f] [filename]");

System.exit(3);

}

br = new BufferedReader(new FileReader(args[1]));

buffer = br.readLine();

while(buffer != null)

{

System.out.println(buffer);

buffer = br.readLine();

}

}

else

{

br = new BufferedReader(new InputStreamReader(System.in));

buffer = "";

System.out.println("Please enter an expression, or type quit");

while(!buffer.trim().equalsIgnoreCase("QUIT"))

{

buffer = br.readLine();

if(!buffer.trim().equalsIgnoreCase("QUIT"))

{

System.out.println(buffer);

}

}

}

//This will display at the end of the program after "QUIT" it inputted

//It will display how many expressions the user put in, which were valid and which were invalid

System.out.println();

System.out.println("+++++++++++++++++++++++++++++++++++++++++");

System.out.println("+ Total expressions:");

System.out.println("+ Total valid expressions: ");

System.out.println("+ Total bad expressions:");

System.out.println("+++++++++++++++++++++++++++++++++++++++++");

}

}

Any suggestions on how to make what i have better?

KHill054a at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 5

If you allow the "QUIT" operator to appear in files as well in console input, then you can simplify your processing. (By the way, that would be a reasonable thing to add even in real-world applications. If "QUIT" can appear in a file, then you can use the stuff in the file following that line for comments, etc.)

So, first you'd process your command-line args. After that's done, you'll have a BufferedReader. Then you use that BufferedReader in a (single) processing loop. You won't need to duplicate the input processing logic in two places.

Other than that, I think this looks like a really good start.

I suppose this is obvious, but anyway a hint: you're almost certainly going to have many more classes that the ones asked for in the assignment. At least one more will implement RationalExpression.

paulcwa at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 6
My password doesn't work for my user name so I had to create another one. It wouldn't even reset it because it says I don't have a security question and answer set. I know I filled those lines in. Why couldn't I sign in?KHill054
Khill0514a at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 7

No, he gives us a file and it can't have QUIT in it.

I'm not following you, with what you said in the second paragraph.

Also, he gave us the code for RationalExpressions. It's:

interface RationalExpressions

{

public RationalNumber add(RationalNumber r);

public RationalNumber sub(RationalNumber r);

public RationalNumber mlt(RationalNumber r);

public RationalNumber div(RationalNumber r);

public boolean lt(RationalNumber r);

public boolean gt(RationalNumber r);

public boolean eq(RationalNumber r);

public boolean le(RationalNumber r);

public boolean ge(RationalNumber r);

public int getNumerator();

public int getDenominator();

}

And for the class RationalNumber, I just have this (so far):

class RationalNumber extends Proj1 implements RationalExpressions

{

}

But it won't compile. It says "RationalNumber is not abstract and does not override abstract method getDenominator() in RationalExpressions

class RationalNumber extends Proj1 implements RationalExpressions" Why is it doing that?

Khill0514a at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 8

>

> But it won't compile. It says "RationalNumber is not

> abstract and does not override abstract method

> getDenominator() in RationalExpressions

> class RationalNumber extends Proj1 implements

> RationalExpressions" Why is it doing that?

Because you implement an interface, but you don't fulfill its contract...

You have to implement EVERY method that is given in the interface to succeed.

But it is not a good idea, to let RationalNumber implement the RationalExpression interface. These are two different things. Also, I don't see why you should inherit from your main class.

Each equation is build of numbers and expressions. You have to model both to form an equation. I think, you should do this first before you worry about in- and output.

I'll give you a hint: write a new class that implements RationalExpressions. Then, write method stubs to all the declaration. And now, comment on EVERY method what you think it should do. You really don't have to implement details by now, but as soon as you see how everything fits together, you can implement each method in no time.

Mongera at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 9
but the problem is, the directions say"A concrete RationalNumber class should be created. This class must implement the RationalExpressions interface."
Khill0514a at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 10

What I meant was, you can do something like this (in pseudo-ish-code):

BufferedReader in;

if (args.length > 0) {

// do some stuff to process file args

in = new BufferedReader(etc);

} else {

in = new BufferedReader(new InputStreamReader(System.in));

}

// now that you've allocated a BufferedReader, use it to evaluate expressions

String line;

while((line = in.readLine()) != null) {

// read expression on line

}

You don't need a separate expression-reading loop for both the file case and the console case.

You can still have a single processing loop, even if you can't have QUIT in the file input (which is a mistake on your professor's part, in my opinion). You can just set a boolean flag indicating whether input is from a file or the console. If it's from a file, then ignore QUIT in the input.

paulcwa at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...
# 11

I think it does make sense that RationalNumber implement RationalExpression. A number is a valid expression (that evaluates to itself). "42" is a valid numeric expression, albeit a trivial one.

The part that doesn't make sense is the stuff that the prof put in the interface. An expression won't necessarily operate on numbers; it can also operate on other expressions. The relational operators should return BooleanExpression or the like. Come to think of it, there ought to be a super-interface just called "Expression". Also I don't see a clear delineation here between rational expressions and rational numbers.

And for that matter I don't see how one would parse an expression and tell whether it's valid given that definition of RationalExpression. I'd expect to see something holding an operator and two more expressions.

But then maybe there's something else here that I didn't see.

paulcwa at 2007-7-14 16:19:08 > top of Java-index,Java Essentials,Java Programming...