Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Get Methods & Issues With Collecting Input for Multiple Users  RSS feed

 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.  I've been working on this for a few hours now.  I had to rewrite my original grade program and write methods for this next project.  I have all the methods written, and they all pretty much functional and return what I need.  My problem now is that my getUserInput() method takes in input for as many people as I want it to, but doesn't report back for each of them, only the last entered.  The counter updates just fine because my output always shows *the last input divided by the proper counter #*.  Any input on what I am missing would be appreciated.  Not asking for the solution, just some guidance.


 
Vasyl Lyashkevych
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I added opportunity to gather all student together

 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!  I am working on it now.  I forgot about arrays.  I haven't had to use them.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. @Vasyl: Please don't provide solutions. In this case, the solution provided isn't even a good one. This is a learning site and if you provide solutions that are not good, that could possibly impede someone else from learning good practices. On the other hand, you will hopefully learn from corrections given to your offered solution. Still, we prefer that you don't give out solutions to OP because it's better that they try to work out a solution themselves. We can guide and give hints.

2. @OP: The problem is that you're storing the new values in the same variables that you had used before. That's why you're seeing only the last values entered. The solution offered,  which you probably gave the thumbs-up to, is certainly one way to resolve that. However, it reminds me of a sign I saw at the shop where I last went for an oil change: "If it's still broken, you didn't use enough duct tape."  You shouldn't use duct tape in a program. If you get into that habit, you'll just keep writing uglier and uglier programs.

3. A more proper solution is to create another class whose instances will encapsulate the scores entered. Who do these scores belong to?  If each set of scores belongs to a Student, then the scores should be in a Student class. So, instead of creating a new array called new_student, you would create a new object by doing this:

Each student will have their own set of score variables. In contrast, the code offered was this:

The problem with that is you are going to be limited to 4 scores, the data is floating around not attached to any object and you have to write separate code/logic to interpret what that String[] means. This is the kind of problem that object-oriented programming is meant to solve in the first place. So, in effect, you are writing code in an object-oriented language that creates the kind of problems object-oriented programming languages like Java are meant to address.

That's like buying a car but instead of driving it, you're hooking a harness up to it and dragging it behind you.

One positive thing to note: Vasil did seem to have the right idea, since he used the name new_student.  At least it hints at what you should be doing. Notice how similar the name new_student is to the ideal code of instantiating a new Student object.

 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate any sort of guidance.  Just a nudge in the right direction.  I'm in an accelerated class which is almost done.  I've been working on this since 8AM.  I've never asked for help before, but I was so happy that I had it written in less than an hour but have been staring at this issue for hours and finally reached out. 
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object-orientation is about keeping related ideas/behavior together with the data/information that they work on.  Your class is called RevisedGrades.  What does that even mean, conceptually?  What does a thing called RevisedGrades have to do with all that data you have declared from lines 5 to 20?

If you have a drawer in your kitchen (and everybody I know has one) where you just dump all the stuff that doesn't really go with any other stuff that's organized in the other drawers, things like paper clips, stamps, pens/pencils,... basically, your junk drawer, then this class kind of looks like that. It has anything and everything dumped in it.

What is the main purpose of this class?  To report on the grades of a class? Then why not call it something ClassGrades or ClassScores or ClassRecord? Choose a name that suggests or describes a thing's purpose. That way you won't confuse yourself and others reading your code.

As I said before, those scores that you're entering, one set of scores that you enter belong to one Student, right? Then put those scores into a Student object, with one Student object holding all the scores for a particular student. You ClassRecord will keep a list of Student objects.  That's another thing the other suggestion got right, at least.

Ask yourself what you can do with a ClassRecord.  You can ask it to go through the list of Students, get each score for say Test1, total all the scores and get an average.  That might be what you had in mind for the getAverageScore() method. That should belong to a ClassRecord class because it deals with all the student scores.

However, this getLtrGrade() method, what is that supposed to do? By the way, please don't abbreviate names like that. It makes it much harder to read. And how much harder is it to type out getLetterGrade anyway? Nobody wants to read code that mumbles and slurs.  Spell things out and help the reader. Don't be afraid of vowels, they won't cause bugs and they won't cause your program to blow up.  Anyway, does a letter grade belong to a ClassRecord? Or do you assign a LetterGrade to each individual score? Does each individual student get a single letter grade for the total of their scores? If so, then the getLetterGrade() method belongs in the Student class, not the ClassRecord class.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

I swear, I am just doing as I was asked in the assignment.  Trust me, the wording is confusing sometimes.  This is exactly as I was instructed:

Rewrite the Week Four program to include the following five methods:

getUserInput
getAverageScore
getLtrGrade
getLtrList
displayClassReport

This is why my head is spinning.  As for Revised, I will change that.  That was just a way for me to differentiate the new program from my old one.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the grades, my original program posted name, average,and grade for each individual as well as a group average and count of how many students received a certain grade.  After I broke it up into methods, now I don't get the correct group average or list of how many got certain grades....and I know this is because of my poor setup.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I guess there's little use in me giving you an entire lecture on proper object-oriented programming if your instructor is giving you instructions that are totally contradictory.

Here's what I can advise you then:

Lay out all the different concepts you're dealing with. Students, scores for each student, letter grades, averages, etc.  If you're breaking out functionality the way those methods suggest, then of course you're going to have to make all that data accessible and shared by all these different methods...

Let me just get this off my chest because I can't stand to not say anything. Those requirements are horrific! That is NOT how you should be writing programs in Java much less requiring students to write code like that in Java. The design you are being asked to implement is nothing more than procedural code written in the guise of object-oriented constructs, i.e. classes, instances, instance fields, and methods.

Now back to what I was saying...

What you seem to be missing is an understanding of scope. Scope basically define how widely a thing can be "seen" or referenced, in what context it can be referenced, and how long it retains its values/identity.  If you are not inclined to go outside of the "fence" that you have been boxed into, then you're going to have to do what I previously called a "duct taped" solution.  You'll have to use a list of arrays. But please, don't use a String[] to represent values that are numbers. Use an int[] or double[] instead.  A Scanner has methods that make it easy to read in user input and treat the values like int or double. See the Scanner API documentation to see which methods are appropriate for the type of data you expect the user to enter.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to limit the scope of variables and keep a handle on their management is to pass parameters and/or use return values from methods. However, I don't know if you have even been taught how to write methods that return values. From the code you posted, it seems like all you know how to write are void methods that operate on shared data.  Here's an alternative design you could consider, if you were allowed to do such a thing:

getUserInput returns a list of int[], each int[] in the list representing a set of scores for one student.

That list gets passed to the getAverageScore() method, which calculates the average score by going through the list it was passed. The method would return a single value, which is the average score (of all scores of all students?)

That single value of average score gets passed to the getLetterGrade() method (go ahead and mumble the name if you want, I prefer to spell out the name you should be using) which takes that score and translates it to a letter. That is the value it returns.

And so on. This way, you can eliminate the need to have so many shared variables at the instance level scope. Just pass parameters and use the results returned by one method as the input to the next method in the process.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.  That was the next direction I was going to go in.  We weren't really taught to do that, per se.  We were given a few things to read.  I was just going back to the materials to attempt it.  This class is basically read and do.  We had more guidance in the way of virtual labs in the beginning.  I'm trying my best.  I was better with C++ back in college over 17 years ago, when I could stop the professor and ask questions....
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a chance to talk to a bunch of students recently who mostly said they were more comfortable with C++ than they were with Java, which their instructor and I found somewhat perplexing and amusing since that statement is so incongruous with our experience. For me C++ has way too many pitfalls and is a tool for programmers who have more skill and discipline in managing their programs, both from the structural aspect as well as the conceptual aspects. I find Java to be higher-level than C++ in many respects. Admittedly, I don't do much C++ coding but I'd be willing to bet I know more about it than the average novice programmer who has taken courses in both Java and C++.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen many examples of procedural Java code over the years, many from students just learning how to program in Java, many just learning how to program at all. One thing they all have in common is that when the code is more procedural than object oriented, the perspective is backwards when you read the code. Object orientation is about objects and behavior, not so much about data. The OO principle of encapsulation is precisely to hide information inside an object so that you don't have to worry about its management and manipulation; let the object take care of it.

Your main focus as a programmer trying to design an OO program is the behavior of each object and their relationships with other objects. Data is simply something that can influence behavior in some way. You should think of objects as being independent entities with minds of their own. They know how to do things.

So, when you have code like what you have in main(), your focus is still too granular. This is you, the programmer, telling the object every little step to do and the order in which those steps are done. It's like you're a puppeteer, manipulating a mindless doll.

Think of it like this: If you go to a shoe store and can't find a particular shoe in your size, you ask a salesperson to find a pair for you in the stock room. That's it. You don't tell them to walk through the stock room door, go to the third rack, second shelf from the top, and grab the fifth box from the right. The salesperson knows how to do his thing better than you do. You should have this kind of perspective when programming objects. Put the logic for all those small steps in a high-level method of the object. Then you can just call that high-level method from main.

The difference in the code may seem trivial but the difference in your perspective is significant. Instead of you micromanaging your object from main, you just tell it to go do its thing. 
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one of the keys to good object-oriented programs is "intentional cluelessness." In other words, abstraction.  When you write something like this:
This is you, the programmer, relinquishing the responsibility of knowing how to "do grades" to the ClassRecord object named cs101. You're intentionally staying "clueless" at this level of the code as to what exactly the act of "doing grades" entails.

On the other hand, when you write something like this instead:
this is you, the programmer, showing that you know more about the object's business of creating a class report than the object itself does. You have now stepped outside of the world of object-orientation and into a strange twilight zone where you're ostensibly using objects but you're really not. You're just puppeteering; you're manipulating data that's inside the object through "holes" in the object that you've created to be able to access that data. These "holes" are the methods you defined in the object's API. Here, you're forcing yourself to not be clueless and you're getting into the object's business. Mind your own business. Let the object do its thing.

It's like one of those compartments that are used to safely handle hazardous materials in laboratories. You put the hazardous material inside the compartment, then you seal it. You then stick your hands into those holes with the gloves inside the compartment and, through those gloves, you can handle the material inside. This is what the second listing of main() is like. You're sticking your hand inside these gloves called getUserInput, getAverageScore, getLtrGrade, etc. to make the object "do" things. In fact, by taking all those low-level methods and making them the API to your object, you are forcing yourself to do all the heavy lifting from outside of the object.

In contrast, the first version of main() above is like hitting the "Go" button on a robot and just watching it go do whatever it has been programmed to do.

Of course, the name "doGrades()" used in the first listing not a very good name but it's one I chose to illustrate the abstract nature of the perspective you should take at this level. I could have chosen a name like doYourThang() but then that might distract from the point I was trying to make. I'd probably just go with the name of the last method you show there since it has a nice, high-level sounding, and sufficiently clueless name, displayClassReport().
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the more "clueless" version of the code you wrote would look like this:

If you chose better names, the program might actually start making sense, like it's telling a story:

If you use the Driver Class pattern, your "clueless" perspective might become even more clear:

Here, you've clearly separated two responsibilities: 1) being the main program entry point and creating and telling an object to do its thing and 2) actually knowing how to do something.  Responsibility #1 is assigned to the GradeReportingProgram class and #2 is assigned to the ClassRecord class.
 
Campbell Ritchie
Sheriff
Posts: 53773
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . students . . . said they were more comfortable with C++ than they were with Java . . .
That is because we were taught procedural coding throughout our schooldays. I was taught procedural coding at the age of five. Admittedly neither Miss Cutler nor I knew it was called procedural coding, but that is what it was. It was the sort of thing I learnt that lays the foundations for procedural thinking for life. Have a little example:-
Miss Cutler wrote:Campbell, what's 1 + 2?
If you start reading procedural code and compare it to school algebra/maths/arithmetic, doesn't it look similar? I think that is why people gravitate towards a procedural way of thinking. Of course, if people will start by teaching procedural coding, they are going to find it even harder to get people to think in objects later on.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you have a point there, Campbell. When the whole "paradigm shift" from procedural to object-oriented happened a couple of decades ago (as far as the majority of the programmer population was concerned at least), there was much more focus given in thinking in terms of objects than what I have observed there is now. Now, I fear the new programmers have been left with the assumption that the shift in mindset has happened and they just need to get with the program.

Unfortunately, the procedural mindset is well-entrenched from earlier in life. Additionally, the novice lacks context and tries to make up for that by looking for context-free rules which are essentially recipes. That is, they are looking for step-by-step instructions on how to do things they don't really understand how to do. I think this is where education is failing students. Not enough teachers are giving their students the proper context in which to understand objects and object-oriented thinking. They are simply giving students some pretty horrible "recipes," as illustrated by the OP's stated requirements, that are very procedural and procedural-thinking in nature.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.  Your comparisons are outstanding, by the way.  I did tweak it a little bit, but obviously still don't know what I am doing.  I will work on it more later trying to implement what you showed me.  Unfortunately, I had to submit my poorly written, not completely functional program last night.  I'll probably get a zero, but hey...I tried.  I am not giving up on it though.  I will make it work, for my own satisfaction.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a really good attitude to take and it's good to see. Too many folks taking up programming don't seem to care too much about doing the right things the right way. When you do something for the wrong reasons, you're probably going to end up doing the wrong things anyway. I hope you keep asking questions and practicing to be able to write proper Java code, despite the things you're being taught that may not be pointing you in the right direction. If you have any doubts about that, just ask questions here. There's always someone here who will set you back on the straight and narrow.

Good luck.
 
Campbell Ritchie
Sheriff
Posts: 53773
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good luck with the marks, anyway.
I worry whenever I hear about people tweaking things. It makes me wonder whether they are guessing. You can guess 1000000× and as long as the guesses are all different, you will have some really good code in there. Somewhere...
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I changed a few methods into get-return ones.  Nothing huge, but I wanted to make sure I could make something work a little better.  Taking baby steps.  I'd show you, but I am at work.  I do appreciate the advice though.  This is my last week of a 6 week course and at least, this was the only time I had issues.  Last week, I had this programs fully functional with just plain code, not the methods thing.  I even managed to make some GUI that validated a login against a list of IDs and Passwords.  Again, baby steps.  I'm not giving up.  I just need to find more time to practice when this is done.  Straight into my next course for my Master's...and I believe this is the only coding class.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go ahead and post your changes. We can tell you if you're on the right track.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I handed in.  3/4 of us did not complete it, so the professor is making a video to help and giving us another opportunity.

 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what have you tweaked so far to make it better?
 
Swastik Dey
Rancher
Posts: 1782
14
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my understanding you need a List here, as you are taking input for multiple students.  The List should be of a type Class lets say Student, and Student class should have 3 variables like score1,score2,score3.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The professor really wants get-return methods, so I managed to get one in there that works.  I'm waiting 9n a codeo he is about to do with more instructions.  3 out of 4 of is never finished it, so he's giving us another shot. 

Yes, the list is what I am having issues with.  I look at sample code and read text about it, but I really need to see and hear it explained in order to understand it, which he is about to do.   Sometimes it all looks like hieroglyphics to me, but occasionally the rosetta stone pops up and boom....I get the concept.  It's taking longer than usual this time.
 
Melissa Zimmerman
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omg. Meant waiting on a video in the above reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!