• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with creating a method  RSS feed

 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a good idea about what I need to do in order to create this next method that searches for a course within the program listed below, but I am a noob and do not have the skills to precisely put it out into the exact words. please someone help? You don't have to give me exact method if you want, just guide me at least. I have another method I have to create as well but first lets work with this.
This is the algorithm stated below:

set flag equal to false
     set index equal to 0
     WHILE (index is less than number of courses in array AND flag == false)
            extract substring
            IF ( string1.equals(string2) == true ) THEN //course found
                        set flag to true
            ELSE  
                        increment index             END IF
      END WHILE
      IF flag == false THEN
            display message the course name was not found
      ELSE
            course name found at position index
      END IF  




Student class:

 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe this?

 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but getting these errors:

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's more likely that you'll want a method like this:
And
would then be
Also you have a variable called "substring" but it is in fact a String not a substring. 'schedule[index]' is a String. What does that String look like? I'm presuming that the string contains a course name and other stuff, so you'd use the substring() method to pluck out just the course name before you compare it to courseToFind.

Where are you populating the 'schedule' array?
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:It's more likely that you'll want a method like this:
And
would then be
Also you have a variable called "substring" but it is in fact a String not a substring. 'schedule[index]' is a String. What does that String look like? I'm presuming that the string contains a course name and other stuff, so you'd use the substring() method to pluck out just the course name before you compare it to courseToFind.

Where are you populating the 'schedule' array?


it compiled properly with your program.  gotta test.  gonna create the menu for it on the driver. 

this is what I have to do:

  Add a course
2.  Search for a course
3.  Drop a course
4.  Print out student's schedule
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:It's more likely that you'll want a method like this:
And
would then be
Also you have a variable called "substring" but it is in fact a String not a substring. 'schedule[index]' is a String. What does that String look like? I'm presuming that the string contains a course name and other stuff, so you'd use the substring() method to pluck out just the course name before you compare it to courseToFind.

Where are you populating the 'schedule' array?


this is what I have to do for searchCourse.  right way:

The Search Course Method

This method will allow a student to search for a course name from their existing schedule. 
Use a WHILE loop that will stop when one of two conditions is true:  either the course is found OR you have reached the last course in the schedule and did not have a match. 
If the course is found then print out the course name and number of credits. 
If the course is not found then print out a message stating the course is not listed on the student's schedule. 
Only search through existing courses.  If the array is not full and you search the entire array your program will have a run-time error.

Here are the specifications for this method:

Public/Private
Method Name
Description
Parameters and Data Type
Return Value and Data Type
Public
searchCourse
Searches through the existing courses in the array for a particular course name.  Displays one of two messages:  Name of course and number of credits if found OR message stating that the course was not found.
Course name:  String.
Void
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like I said I'm a noob

working on menu to add to driver for searchCourse prompt:

this far right now






what should I do?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instructions don't say anything about using courseCredits as one of the searchCourse() method parameters. If/when you find the course your supposed to print out the course and the credits.

Your method needs to loop through all the entries in you schedule array. So far, unless you've changed it, you are not populating the array, so a search is not very meaningful.
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your instructions don't say anything about using courseCredits as one of the searchCourse() method parameters. If/when you find the course your supposed to print out the course and the credits.

Your method needs to loop through all the entries in you schedule array. So far, unless you've changed it, you are not populating the array, so a search is not very meaningful.


do I have to do the index thing and create an array of 6 courses because that is the max you can have.  the variable has already been created.  just mention it in this part?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the current state of your searchCourse() method? Show the whole thing, don't cut off the beginning or end of the method.

You've allocated an array to hold the schedule up to 6 classes. You have not yet assigned a course to any of those 6 slots.

Also show us your addCourse() method.
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Can you show us the current state of your searchCourse() method? Show the whole thing, don't cut off the beginning or end of the method.

You've allocated an array to hold the schedule up to 6 classes. You have not yet assigned a course to any of those 6 slots.

Also show us your addCourse() method.


here have the whole thing:



  
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"that student class:"

is not meant to be part of the code
accident on here
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to format your code correctly.  It is going to help your coding and debugging a lot.

You're calling the searchCourse() method like this:

But your method signature looks like this:
So you see you have declared the method to take two parameters but you call it with only one.

But before you change that, don't you want the method to return the courseName?  If so, you should declare searchCourse() to return s String, then add a return statement to your searchCourse() method.

There are several other problems with the code, but let's start with that.
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You really need to format your code correctly.  It is going to help your coding and debugging a lot.

You're calling the searchCourse() method like this:

But your method signature looks like this:
So you see you have declared the method to take two parameters but you call it with only one.

But before you change that, don't you want the method to return the courseName?  If so, you should declare searchCourse() to return s String, then add a return statement to your searchCourse() method.

There are several other problems with the code, but let's start with that.


I'm not fully understanding what you want me to do exactly
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you extract the course name, you want to place it in a temporary string to compare it to the course you are searching for.  Set it up like so:
            tempString = schedule[index].substring(0,6);
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Palazo wrote:I'm not fully understanding what you want me to do exactly
Okay, I'll be more explicit.

Use a return type, not a parameter.  When you call a method and you want to get something back from it, use a return type.  This is how the method should be called:

And this is how the method should be declared:

The String just before SearchCourse() shows you that the method returns an object of type String.  Now we have to actually return something in the method.

Add a return statement in the method.  The return statement declares what variable will be returned.  In this case it will be the course name that was found.

But we still have a problem.  If flag is false, there is no return statement.  We need to add one, but what should we return?  For now, let's return null.  (Note: there are some programmers who think that you should never return null from a method, but the fix for this is too complex for now.)

Now you need to check for null when you call the method.

There is still a lot that you could do with this method and the rest of the code, but let's do this first.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!