Results 1 to 4 of 4
  1. #1
    freethinker89 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Unhappy 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++;
    }
    }
    }
    }

  2. #2
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    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.

  3. #3
    freethinker89 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    freethinker89 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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++;
    }
    }
    }

Similar Threads

  1. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  2. Replies: 0
    Last Post: 08-28-2008, 05:14 PM
  3. Replies: 0
    Last Post: 04-15-2008, 01:32 AM
  4. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 11:55 PM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •