File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes need help: I'm just not getting how this whole Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help: I Watch "need help: I New topic
Author

need help: I'm just not getting how this whole "private" / "public" class definition thing works.

Holden Hodgdon
Greenhorn

Joined: May 24, 2011
Posts: 4
We are using absolute Java 4th edition for this class, and chapter 4.1 prattles on for 32 pages of run on sentences. I've read 4.1 three times now, and all of chapter 4 twice, but I'm still not getting this ... I have this funny feeling that this book is kinda written as a guide to Java for people who already know how to program, I don't so instead of frolicking outside in the summer sun I'm pulling my hair out in front of a monitor.

additionally the teacher isn't "accessible" ...
(*slaps self* stop bitching!)

ok, so what I'm understanding is that you establish either public or private class "entities" outside of where the actual code is run. from there you can then use those established "classes" to dynamically generate instanced variables based on the root classes, that way you don't have to hard code in multiple copies of the same thing.

I get that (I think), but how the hell do I actually go about implementing that? the book has got a bunch of examples of different ways to go about this, but every single one of them only highlights the "no " parts of things while completely ignoring the "WTF is that doing" side of things.

I'm totally lost on how to establish the various elements of these self defined classes, that each instanced variable inherits from the parent class.

for example:

in the assignment that I'm working on (and is subsequently late at this point), we have to create a student record that calculates a final grade based on inputed supplied test scores.

the whole list of data we have to deal with is naming the student record, 3 quizes, 2 tests, the number of points the student got for each one of those, and the final grade.

I sorted that down to what I think I need:

the class it's self is the student's ID.

attached to that is the total number of grade points they got for the tests, and the final grade.

I've got the actual code to calculate the total points and then generate the final grade built, but it's currently just hard coded to run through one student at a time, and I'm lost on how to build the mechanic for generating instanced classes.

(this is project 6 of chapter 4 [P.248] if anyone wants to look up the un-bastardized details)

could someone provide me a with a walk though on how to set up these classes, and then instance them inside the actual code? preferably with some examples that start at just the basic formating of "this goes here, then that goes there" and then ratchet up more complicated implementations?

I'm kinda bright, and can pick things up quickly provided I've got something to get traction on ... currently this book is like Glass to me.

(P.S. I know beggars can't be choosers, but I sort of have to get this in by midnight tonight or I won't get any credit for it, so I'm gonna be hovering over this thread like a hawk and so even if you can't exactly answer this for me I am willing to take any discussion people throw out.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

on a very high level, (and forgetting about public and private), when you first approach a problem like this, you kind of talk through it. Tell yourself in words what needs to happen. Then, look for the nouns and verbs. Nouns become objects and variables. Verbs become methods (again, this is VERY broadly speaking).

some of the nouns you talk about are:
Student record
Final grade
test scores
quizzes
tests
points

Some of the verbs are
input (scores/points)
create (student records)
Calculate (final grade)

So now you start looking at this and say "What goes together? What can be eliminated? What else might I need?"

Personally, I would start by saying that test and quizzes are more or less the same thing - each has a 'max possible score' and a 'what the student scored'.

This implies i need a class, which I will call 'ExamResult'. It will have two member variables to hold those two items:



So now I need to think about what i need to be able to DO with an ExamResult. I probably need a way to create one, to input the scores, possible change the scores (if i discover I made a mistake on one), and maybe get the percentage. So, I'll revise my class





Now I have a usable examResult class (Note: i have done ZERO compiling/testing on any of this, so don't count on it being actual, usable code). Next I would develop my Student class that can (among other things) hold many ExamResult objects.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Ok, let's see if I can help you so that we both can go frolicking outside in the sun... ;) although I don't understand exactly what you mean, this sounds a bit complicated:
Holden Hodgdon wrote:ok, so what I'm understanding is that you establish either public or private class "entities" outside of where the actual code is run. from there you can then use those established "classes" to dynamically generate instanced variables based on the root classes, that way you don't have to hard code in multiple copies of the same thing.

And I don't have the book that you're using.

When you design classes, you'd normally first start thinking about the real-world things that you want to model in your program, and what you can do with these things. You could create a class for each thing, and add a method to the class for each action that you can do with the thing. Things also have properties. Those will map to member variables of the class.

The keywords public, protected and private are used to control access to the member variables and methods of the class. When designing classes, it's important to carefully think about what the class exposes to the outside world. Normally you want a class to expose as little as possible - make as few things as possible public, because the smaller the number of public things in a class, the easier the class will be to use by other parts of the program, and the more it will be decoupled from other parts of the program. Loose coupling is important, because it makes components of the program more independent of each other, which will make the whole program simpler.

So, it's a good idea to make member variables and methods private, unless there is some good reason to make the member variable or method protected or public.

Can you explain in a bit more detail what exactly the requirements of the assignment are, what you had in mind and exactly where you're hitting the wall?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Holden Hodgdon
Greenhorn

Joined: May 24, 2011
Posts: 4
thats pretty much what I did.

I've got a int for the total score.
ints for each test sore that are then tallied up and applied as the value for the total score.
and then a string for the the final grade (A-F) that is established off of the total score.

there is also a string floating around for the student's ID.

my flow is:


I want / need to take those last three variables (student ID, point score, and final grade) and break them out into their own private clases so that this code could be reused to generate multiple entries into a database of point scores and final grades listed under student ID.

in order to start building a class like that would I do something like this:


or like this


edit:

Jesper de Jong wrote: ...


here is what I got so far:



I don't think I'm going to be able to use that wholesale after setting up the classes that the project wants me to, but thats the flow I'm aiming for.

the project requires that I
define and use a class for the student record. the class should have instance variables for the quizzes, mid term, final, over all numeric score for the course, and final letter grade. the over all numeric score is a number in the range of 0 to 100 which represents the weighted average of the student's work. this class should have methods to compute the over all numeric grade that set the appropriate instance variables. your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. you may add other methods if you wish.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

define and use a class for the student record. the class should have instance variables for the quizzes, mid term, final, over all numeric score for the course, and final letter grade. the over all numeric score is a number in the range of 0 to 100 which represents the weighted average of the student's work. this class should have methods to compute the over all numeric grade that set the appropriate instance variables. your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. you may add other methods if you wish.

I didn't look at your code at all, but this seems pretty clear...

define and use a class for the student record.

ok. so that tells me to do this:


the class should have instance variables for the quizzes, mid term, final, over all numeric score for the course, and final letter grade.

so now do that:


the over all numeric score is a number in the range of 0 to 100 which represents the weighted average of the student's work.

That tells me I may need a method that computes this score, and that it gets called whenever a new test score is added...

etc.
Holden Hodgdon
Greenhorn

Joined: May 24, 2011
Posts: 4
k:



... that helps, now how do I start instancing that class? the book hops around all over the place on this part so I don't have a consistent example to get a grip on what does what.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

you call it's constructor - which is a method you may have to write yourself



So what you probably need is to have in your main some code that loops to get user input. somthing like

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Getting better, but there are still things which can be improved.

You can simplify your if else blocks rather like thisDon't use tabs for indenting and don't write long lines in your code; they are difficult to read. Use spaces for indents; you can probably get your text editor to convert tabs to spaces automatically.
Never never write == true or == false. If you want something to happen when b for "boolean" is false, write while (!b) { . . . }
Your gateA boolean variables are poorly named; you can't tell by reading them what they mean. Maybe something likes finishedEntry3 would be better. When you have the String for finishing the loop, you can get a neater assignment like this: finishedEntry3 = "y".equalsIgnoreCase(gateC1); There is a reason for writing the equality test backwards; the bit on the left can never be null.

You ought to get all those values out of the main method and into a class. Your StudentRecord class should look something like thisAlso I think you are making things difficult for yourself by trying to do too much all at once.

Get that sort of thing working before you try any of the loops.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help: I'm just not getting how this whole "private" / "public" class definition thing works.