my dog learned polymorphism
The moose likes Beginning Java and the fly likes Make methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Make methods" Watch "Make methods" New topic

Make methods

Tom Cummings

Joined: Nov 08, 2012
Posts: 7
I can't figure out how to make the methods I need for these ...


this is the tester for the above class
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

What are they supposed to do? Surely someone has given you some idea or specs regarding how they need to be implemented.

And technically, the methods have been made. They are all right there on lines 31-40. Granted, they have probably not been properly IMPLEMENTED, but we have no way to know what they are supposed to do.

If you explain that to us, then you'll know how to implement them yourself.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
If you are still at the stage of knowing how to make methods, you ought only to write a few lines. Create some class to practise on. Create a few methods and make another class with a main method to test them out. Here’s a possible classAdd some methods. How about a void celebrateBirthday() method, which makes you one year older? How about int getYoungestPartnerAge(), which returns age ÷ 2 + 7 ? How about a Party class, and a Couple class? How about a method in the Party class, which creates two Person objects and sees whether age ÷ 2 + 7  applies, and if so, creates a Couple object from them? (Work out what to do if it doesn’t apply. Maybe return null?)

I shall stop making such silly suggestions and refer you to the Java Tutorials, where you will find a little more about methods.
Tom Cummings

Joined: Nov 08, 2012
Posts: 7
Well the output needs to look like this ...

Sample input data file
Michael 70
Dondiego 80
Troy 100
Derrick 55
Richard11 60 //name is invalid
Jamel 80.50 //score is invalid
David 99
Thomas 85
JohnAnthony 10
Devin 25
Antwann 66
Christopher 55
Erin 82
Sample output of the program

<Your name here>
Class Name: <Enter your class name here>
Instructor Name: <Enter your instructor>
Semester: <Enter current Semester
School Name: <Enter your school here>
Class Size: 0
Highest Score: -999
Lowest Score: 999
Average Score: 0.0
<Your name here>
Class Name: CS3410
Instructor Name: Smith
Semester: Spring
School Name: Georgia Institute of Technology
Class Size: 10
Highest Score: 100
Lowest Score: 10
Average Score: 64.5


1 Michael 70 C
2 Dondiego 80 B
3 Troy 100 A
4 Derrick 55 F
5 David 99 A
6 Thomas 85 B
7 JohnAnthony 10 F
8 Devin 25 F
9 Antwann 66 D
10 Christopher 55

That's all I was given I made the methods but ,yes I do not know how to implement them correctly.
Tom Cummings

Joined: Nov 08, 2012
Posts: 7
I have came up with this ....

But it still is not working right
Tom Cummings

Joined: Nov 08, 2012
Posts: 7
- This project should accomplish the following tasks:
1. Reading from a text file data13.txt (entered as an argument). The file contains names and integer scores. One name and a score per line. Names are supposed to be alphabet and scores are supposed to be integers. Assume the input file has up to 100 names and scores.
2. Scan each token from the file. If name or score is invalid, do not store them in names and scores arrays. .
3. Implement MyArrays13 class as specified in the UML diagram below. NAME IT

- name : String
- className: String
- instructorName : String
- semesterName: String
- schoolName: String
- highestScore: int
- lowestScore:int
- averageScore: double
- size: int
- integerPattern: String
- alphbaetPattern: String “Your name>
“Your class”
“Your Instructor”
“Valdosta State University

+MyArrays13(className: String, instructorName:String, semesterName:String, schoolName:String)
+readAndStore (file:Scanner,names:String [], scores[]:int) : void
+printClass (names:String[], scores: int[], grades: char[]):void
+computeGrades (scores:int [], grades: char []):void
+highestScore (scores: int []):void
+lowestScore (scores: int []):void
+averageScore (scores: int []):void

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Tom Cummings wrote:I have came up with this .... . . .
I am afraid I didn’t even read it all. When people say that sort of thing, I get all suspicious and think they are guessing. You can guess a million times and maybe hit the right answer.

Or you can work it out and get the right answer first time.
Get yourself a pencil and paper and go through exactly how you are going to achieve that. If you are using a switch on percentage / 10 (not a good idea until you are familiar with methods and division), write down exactly what you get if you divide the limiting marks. After the end of that switch block and before the end of the loop, put in some testing code, eg
System.out.printf("At score %3d%% the grade was %s%n", scores[i], grades[i]); //test
When you are happy that bit is working all right, you can move on to the next bit.
Look for declarations. You can tell a declaration whenever it has a type, eg int written in. Make sure you never declare the same thing twice by mistake.
Have you been told to use parallel arrays? That is usually a very bad idea. Why did they not tell you to create Student objects to encapsulate the mark, grade and name?
There is some poor formatting; you have two }} on the same line. That is a sure-fire way to lose count of your braces and get errors you cannot understand.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

more advice: work on only ONE method at a time. The whole point of having methods is that they are independent of each other. Your computeGrades method really doesn't care how you get the data - you could read it from a file, pull from a database, open a socket to a different server, or just about anything else.

So pick one of those three methods. Get it to work before you consider writing any of the others.

Whichever you work on first, you will write a lot of code you eventually throw away to prove that it works. I probably write 5 System.out.println() statements for every line of code I end up keeping. I validate methods get called, and what they return. I prove that my computations are correct. I show that the data is being stored.

So, perhaps you should start with the readAndStore method. What does it need to do? Break it down into discrete steps (which might imply you need additional methods). Once that works, then you can use it to build/test the next method.
It is sorta covered in the JavaRanch Style Guide.
subject: Make methods
It's not a secret anymore!