• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polynomials into Array using user input with scanner.  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking a online Java course and attempting my 2nd assignment. The first assignment was not difficult but this one is just way over top. I honestly don't know where to begin. The problem is this class is online, basic instructions are to read these chapters and then write the program. There's not much guidance since my instructor is not with me physically.

I've never used scanner before or created two dimensional arrays. My instructor gives notes on what to do in each area of his skeleton program but I don't have a clue which one to start with.

Assignment:
Your task is to implement a similar scheme to store polynomials of any number of terms, such that the number of terms and the components (coefficient, variable and exponent) of every term are entered from the keyboard. To implement the interactive input we will using the Java class Scanner, defined in the java.utils standard package. The Scanner class can be used in Java to read data types from a file. Since the input console (keyboard) is treated as the file called System.in, we can create a Scanner for that input stream as new Scanner (System.in), as shown below. Once you define a Scanner object, using its method next() you can read Strings from the file/keyboard. The incomplete program below is your assignment. You are supposed to complete without changing the existing code. Your output should be the terms of the polynomial entered by the user, separated by + signs. Additional instructions in the code below, that you will change to achieve the requested functionality.




Anyways, I'm not looking for straight answers. I just want to know which section I should start on first because I was advised to tackle programs one problem at a time. And I'll post back my work as I go along. Thanks in advance guys.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest start with none of them. I suggest you start with a Scanner object and create a class which reads from the keyboard and displays the result.
Beware of the difference between entering 123 and "123". One is an int and the other a String.
Suggested part of your class:-In my opinion that is not the best way to use a Scanner. The best way to use a Scanner is to wrap it in a utility class which chooses the methods and can even deal with the problem about nextXXX followed by nextLine. But that is not an issue here, because you have been told to do it a particular way.

I suggest, once you have some familiarity with Scanner, you write another class which creates arrays of different sizes and prints them out, so you can see their workings. (Unless you are already au fait with arrays.Keep playing with that sort of code. See what sort of changes work and what sort causes Exceptions.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There ain't no such thing as a 2D array. What you can have, however, is an array of arrays:-Links:
Arrays#deepToString() (and rest of Arrays class).
Java Tutorials (arrays)
Java Tutorials: look for “Scanning”
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Huynh wrote:I'm taking a online Java course and attempting my 2nd assignment. The first assignment was not difficult but this one is just way over top. I honestly don't know where to begin. The problem is this class is online, basic instructions are to read these chapters and then write the program. There's not much guidance since my instructor is not with me physically.

That is difficult; and I have to say that I don't like the exercise, because it's likely to involve a LOT of unnecessary conversions to and from Strings.

However, just from what I can see from a cursory glance:
1. The business of Polynomials is a red herring. The fact is that you are entering a list of Strings in the form of groups (the instructor calls it a 'term'), where each group has 3 components: a coefficient, a variable name, and an exponent.

2. A String[][] is simply an array of String arrays - ie, an array where each element is a String[]. I presume you've dealt with one-dimensional arrays before now.

3. Unfortunately, the 'coefficient' and 'exponent' elements of each group are also numbers (even though they're stored as Strings), so you will need to ensure that what was entered really is a number. You might want to have a look at the 'parse...' methods in the Java wrapper classes (eg, Integer.parseInt(String)) to help you do this (see also point 7).

4. That storeTerm() method signature looks horrendous because it would appear that, having gone to all the business to convert the relevant input Strings to numbers, you now have to convert them back to Strings in order to store them in your poly array. However, if that's what you're supposed to do, you should do it - the only thing I can suggest is that you keep the same element order as for input (ie, 'coeff' should go in poly[where][0] and 'exp' should go in poly[where][2]...converted back to Strings, of course).

5. (a reminder) Java array indexes start at 0. It's hard to remember when you're starting out, but eventually you'll grow to love it.

6. Be careful of that loop at line 53, because it starts at 1.

7. Consider writing getCoefficient(String[] term) and getExponent(String[] term) methods to convert those element Strings to numbers. It's not absolutely essential, but I think you'll find it makes your code more readable.

HIH. And good luck.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!