|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

10-06-2008, 11:51 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
|
Understand my logic errors and better understanding method and class creation
Hi, I'm a student, and yes, I'm looking for help with and assignment. However, I'm not looking for an easy way out of my assignment... I'm just lost. I need help understanding how I need to change my code.
I particularly need help here:
//** The method displayHeading displays your name, BlazerID, course name, and your
//** row number and seat number. The course name is passed as a parameter. ** 1 point
myObject.displayHeading("CS-201");
public void displayHeading(String course)
{
System.out.println("\n\nBen Giles");
System.out.println("bengiles");
System.out.println(course);
System.out.println("Row Number: 2 Seat Number: 5\n");
}
I'm pretty lost. I don't understand what's going on exactly. I know this is simple, but I don't really understand methods. I'm reading the tutorial on Classes at java.sun.com/docs/books/tutorial/java/ right now, so maybe I'll understand what's going on at this point in the code before anyone answers. However, any explanation about this section (and my mistakes) would be greatly appreciated. Thank you!
Here's the full thing:
import java.util.Scanner;
public class MyClass456
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
MyClass456 myObject = new MyClass456(); // Replace NNN with 456.
// At the actual exam use your test number.
int n1, n2, n3, i;
//** The method displayHeading displays your name, BlazerID, course name, and your
//** row number and seat number. The course name is passed as a parameter. ** 1 point
myObject.displayHeading("CS-201");
public void displayHeading(String course)
{
System.out.println("\n\nBen Giles");
System.out.println("bengiles");
System.out.println(course);
System.out.println("Row Number: 2 Seat Number: 5\n");
}
//** method testSigns(n1,n2,n3) returns the String:
//** "All Positive or Zero" if all three parameters are zero or positive ( >= 0 )
//** "All negative" if all three parameters are negative ( < 0 )
//** "Postive and Negative" if parameters are mixed signs ** 4 points
//** Example: calling testSigns(-1,3,0) returns "Positive and Negative"
//** Example: calling testSigns(10,13,0) returns "All Positive or Zero"
//** Example: calling testSigns(-2,-7,-12) returns: "All Negative"
for (i = 0; i < 3; i++) // test method testSigns three times
{
System.out.print("\n\n** method testSigns **\n\nEnter the first positive," +
"negative, or zero integer: ");
n1 = scan.nextInt();
System.out.print("\nEnter the second positive, negative, or zero integer: ");
n2 = scan.nextInt();
System.out.print("\nEnter the third positive, negative, or zero integer: ");
n3 = scan.nextInt();
System.out.println("\n" + n1 + " " + n2 + " " + n3 + ": " +
myObject.testSigns(n1, n2, n3));
}
public String testSigns(int x, int y, int z)
{
if (x >= 0 && y >= 0 && z >= 0)
return "All Positive or Zero";
else
if (x < 0 && y < 0 && z < 0)
return "All Negative";
else
return "Postive and Negative";
}
//** Method oddNumbers(n1, n2) displays the odd numbers
//** from integers n1 to n2 (inclusive)
//** The odd numbers are displayed one per line.
//** Examples: oddNumbers(1,5) displays: 1 oddNumbers(10,18) displays 11
//** 3 13
//** 5 15
//** ** 5 points 17
System.out.print("\n\nEnter the first integer: ");
n1 = scan.nextInt();
System.out.print("\n\nEnter the second integer: ");
n2 = scan.nextInt();
myObject.oddNumbers(n1,n2);
System.out.println();
public void oddNumbers(int start, int finish)
{
while (start <= finish)
{
if (start % 2 != 0)
System.out.println(start);
start++;
}
}
}
}
|
|

10-07-2008, 12:24 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
one thing I Noticed is you have a whole method in the actual program, they should be outside the main program and declared in the actual main. for example
Public static main(String [] args)
{
exampleMethod();
}
public static void exmpaleMethod();
{
// the crap u have in side here
}
that is the proper way to do a method on declaring it, I am not sure if that is the proglrem i just noticed ,that I dont have much time to look at it, but I will try tonight whe nI get off work.
|
|

10-07-2008, 12:32 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
|
Thanks so much madatlis. I'm going to look back at my code think about what you said.
This might be easier, since the code's formatting is messed up. Here is our assignment, the skeleton of the test, and the solution...
h t t p : / / w w w . cis.uab.edu/ranelli/cs201/Lab6f08/lab6.html
(I would link to it, but apparently I need more posts before I can link.)
I don't understand how to insert the solution into P.java.
|
|

10-07-2008, 01:03 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
Yeah! I've been struggling with this thing since last Friday, and after moving things around it compiled and ran correctly. Thanks again!
However, I want to make sure I really comprehend what the error was there, so if you (or anyone else) could explain what was wrong with a little more detail, I'd be so grateful.
Here's my code now:
//
//
// CS201 Practice Lab Midterm Exam
//
//
import java.util.Scanner;
public class MyClass456
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
MyClass456 myObject = new MyClass456(); // Replace NNN with 456.
// At the actual exam use your test number.
int n1, n2, n3, i;
//** The method displayHeading displays your name, BlazerID, course name, and your
//** row number and seat number. The course name is passed as a parameter. ** 1 point
myObject.displayHeading("CS-201");
//** method testSigns(n1,n2,n3) returns the String:
//** "All Positive or Zero" if all three parameters are zero or positive ( >= 0 )
//** "All negative" if all three parameters are negative ( < 0 )
//** "Postive and Negative" if parameters are mixed signs ** 4 points
//** Example: calling testSigns(-1,3,0) returns "Positive and Negative"
//** Example: calling testSigns(10,13,0) returns "All Positive or Zero"
//** Example: calling testSigns(-2,-7,-12) returns: "All Negative"
for (i = 0; i < 3; i++) // test method testSigns three times
{
System.out.print("\n\n** method testSigns **\n\nEnter the first positive," +
"negative, or zero integer: ");
n1 = scan.nextInt();
System.out.print("\nEnter the second positive, negative, or zero integer: ");
n2 = scan.nextInt();
System.out.print("\nEnter the third positive, negative, or zero integer: ");
n3 = scan.nextInt();
System.out.println("\n" + n1 + " " + n2 + " " + n3 + ": " +
myObject.testSigns(n1, n2, n3));
}
//** Method oddNumbers(n1, n2) displays the odd numbers
//** from integers n1 to n2 (inclusive)
//** The odd numbers are displayed one per line.
//** Examples: oddNumbers(1,5) displays: 1 oddNumbers(10,18) displays 11
//** 3 13
//** 5 15
//** ** 5 points 17
System.out.print("\n\nEnter the first integer: ");
n1 = scan.nextInt();
System.out.print("\n\nEnter the second integer: ");
n2 = scan.nextInt();
myObject.oddNumbers(n1,n2);
System.out.println();
}
public void displayHeading(String course)
{
System.out.println("\n\nBen Giles");
System.out.println("bengiles");
System.out.println(course);
System.out.println("Row Number: 2 Seat Number: 5\n");
}
public String testSigns(int x, int y, int z)
{
if (x >= 0 && y >= 0 && z >= 0)
return "All Positive or Zero";
else
if (x < 0 && y < 0 && z < 0)
return "All Negative";
else
return "Postive and Negative";
}
public void oddNumbers(int start, int finish)
{
while (start <= finish)
{
if (start % 2 != 0)
System.out.println(start);
start++;
}
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|