• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Class Assignment

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, this is my first post here and I'm working on an assignment. I've gotten pretty far but have gotten stumped and need some help.

For this assignment you will want to create 2 files called "RunMyMethods" and “MyMethods” The first file “RunMyMethods” will contain the main that calls a menu method and will have a loop structure that will allow a user to choose what program they want to run. The menu displayed will look like this:

1) Product no negatives

2) Find Twelve

3) Max Min Avg

4) Letter Grade

5) Custom Method ( you design and name)

6) Exit

The second file “MyMethods” will have methods in it that correspond to the menu shown above.  The methods should do the following:

1.

Write a program that will find the product of a collection of data values. The program should ignore all negative values, not convert negative values to positive. For Example if I enter in 2 -3 4 the answer would be 8 not positive 24.

2.

Write a program to read in a collection of integer values, and find and print the index of the first occurrence and last occurence of the number 12. The program should print an index value of 0 if the number 12 is not found. The index is the sequence number of the data item 12. For example if the eighth data item is the only 12, then the index value 8 should be printed for the first and last occurrence.

3.

Write a program to find the minimum, maximum, and average values of a collection of data values. The number of data values is unspecified.

4.

Write a program to read in a collection of exam scores ranging in value from 0 to 100. The program should count and print the number of A's (90 to 100), B's (70 to 89), C's (50 to 69), D's (35 to 49) and F's (0 to 34). The program should also print each score and its letter grade. The following is an example of test data you could enter, but feel free to enter any data you like:
63 75 72 72 78 67 80 63 0 90 89 43 59 99 82 12 100 75







MyMethods


   





RunMyMethods





 
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Travis!

Just to let you know, when you post code here you need to UseCodeTags (<- read this link). I'll fix it for you this time.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put empty lines between successive methods.
Why are you using StringTokenizer? Have you read its documentation? You can use a Scanner to get the tokens you want, and that doesn't need you to split the input, nor to mess about with Integer.parseInt(). Simply use nextInt().
You only ever need one Scanner reading System.in,  You can probably make that a static field which you use in all your code. You don't need multiple objects reading from System.in. Don't mix Scanners and buffered readers reading the same input.
Don't use multiple assignments all on the same line (line 48).
Don't write methods which do nothing (lines 22‑24).
Query line 35 with your teacher. I think it doesn't say to accept positive values, but to reject negative values. You should therefore accept 0 as a valid input, even if it means you will get 0 as a result.
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a bit weird to me that StringTokenizer has never been marked @Deprecated in the javadocs, possibly because it was considered deprecated long before they had annotations??

Instead they just leave these words in that everyone skips on their "Race To the Bottom":
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
They even show the example using .split()

Anyway, only the most "complete" treatments of Java API's have mentioned it for ages.
When they do, they normally describe it as "Deprecated" and I only just right now realized it isn't marked as such.

I agree with everything Campbell said, and just added this because I was going to remind them to always check for "Deprecated since" when reading Javadocs, only to find they never bothered to deprecate this ancient relic for some reason or another.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashtable, Enumeration, Vector and Stack are also considered legacy classes which nobody uses any more, but aren't marked @Deprecated.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Query line 35 with your teacher. I think it doesn't say to accept positive values, but to reject negative values. You should therefore accept 0 as a valid input, even if it means you will get 0 as a result.

If you look at the example formula in the requirements it is actually using the absolute value of any negative number.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, absolute values can cause just as much nasty confusion, can't they.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not confused (?). But the results are different than just ignoring negative numbers.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it was in the first part of the assignment, I thought it said to ignore negative numbers rather than taking their absolute values.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For Example if I enter in 2 -3 4 the answer would be 8 not positive 24


Aaaaaaarrrrrrggggg - some day I'll learn to read ;)
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never let that sort of thing worry me. I simply apologise whenever I make such a mistake.
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Hashtable, Enumeration, Vector and Stack are also considered legacy classes which nobody uses any more, but aren't marked @Deprecated.



Well, Enumeration and Vector are still in use by some Swing classes -- the DefaultMutableTreeNode class provides Enumerations which allow you to traverse the tree, and JList uses Vector to store the list of objects which it displays. So until they deprecate Swing, it would be annoying for them to deprecate Enumeration and Vector at least.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At least Enumeration got a bridge method in Java 9, tho:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Enumeration.html

I am surprised at the number of people who work full time in Java (and also teach it) that claim to have no realization that Enumeration even exists.

Maybe I shouldn't be.  It comes up when they refer to an Enum as "an Enumeration" and I say "It is better to just say Enum so people don't think you meant the Enumeration class" and they reply "What the heck is THAT?"

Of course, as of Java 8, that method didn't exist, so...yeah.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . .  still in use by some Swing classes . . .

Was that before or after the latest update to Swing? I mean the update done by Ada Lovelace hereself They haven't deprecated AWT yet, and that was last updated by King Alfred. It took such a long time that the cakes burnt while he was doing it.
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:At least Enumeration got a bridge method in Java 9, tho:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Enumeration.html



Yes, after I (and no doubt hundreds or thousands of other people) already wrote a method which does that. Such a method makes it possible to feed the Enumeration into a Stream and apply functional processing to it. And it's almost trivial to write.

And yes, I subsequently changed my code to use the Java 9 Enumeration.asIterator method instead of the method that I wrote to convert an Enumeration to an Iterator.
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Was that before or after the latest update to Swing? I mean the update done by Ada Lovelace hereself They haven't deprecated AWT yet, and that was last updated by King Alfred.



As they say, it ain't over until the fat lady swings. Although Countess Lovelace doesn't look particularly fat in her photograph.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of hijacking the original poster's thread, I will just say that the various methods that aren't marked deprecated in the javadocs and annotated as such to generate warnings in IDE's...that they continue to get used as much as they do despite the exposition at the top of the Javadocs saying basically "Yeah, don't use this, man!" validates my goofy "Race to the Bottom" theory.

Many purveyors of tutorial material fly right thru that stuff and just mention all the API calls, completely ignoring important warnings and recommendations that come before the first of the constructor and method explanations.  If you watch enough videos, you can watch these things literally scroll right by while the instructor is talking rapidly...

Then those who learn from these sources are soon seen here, using no-longer-recommended classes and ignoring dire javadocs warnings, etc. as a result.

I mention these things to said purveyors when I notice them.  They are at the Top of the Waterfall, we are at the bottom watching the barrels smash.
 
Sheriff
Posts: 17633
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Travis Bergkoetter wrote:Hi guys, this is my first post here and I'm working on an assignment. I've gotten pretty far but have gotten stumped and need some help.


Please be more specific in stating what kind of help you need. Without clear specifics, people will just comment on whatever they are interested in rather than what you actually need help with.

What isn't working the way you expect it to work? Be specific in pointing out what you tried, what you expected, and what you got instead. What concepts do you not understand?
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic