Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Can you guys help me fix my assignment

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you guys help me fix my assignment

    this is the program that i need to do

    You are required to write a command line program in Java that declares three numbers.
    Write a method called calc() that takes in the three numbers as parameters. Inside this
    method, you must add the first two numbers together and divide the answer by the
    third. The value of the sum must be returned. Write another method called display()
    that prints the following to the screen:
    ******************************************
    * *
    * This is a simple program *
    * that performs a mathematical *
    * calculation. The answer is: *
    * [answer] *
    ******************************************



    so far i made this.

    import java.util.Scanner;

    public class assignmentB
    {
    public int calc(){

    int number1;
    int number2;
    int number3;
    int sum;

    Scanner scan=new Scanner(System.in);

    System.out.println("Please enter 1st number: ");
    number1 = scan.nextInt();

    System.out.println("Please enter 2nd number: ");
    number2 = scan.nextInt();

    System.out.println("Divide the sum of the two numbers by: ");
    number3 = scan.nextInt();

    sum = (number1 + number2)/ number3;

    return sum;

    }


    public String display(){

    System.out.println("****************************** ***");
    System.out.println("* *");
    System.out.println("* This is a simple program *");
    System.out.println("* that performs a mathematical *");
    System.out.println("* calculation. The answer is: *");
    System.out.println("* "+ sum +" *");
    System.out.println("* *");
    System.out.println("****************************** ***");

    }
    }



    can anyone fix this for me? so it will work as state above.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can you guys help me fix my assignment

    Can you explain what the code does now, post its output and add comments showing what is wrong with the output and show what you want the output to look like?

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you guys help me fix my assignment

     import java.util.Scanner;
     
    public class assignmentB
    {
    public int calc(){
     
    int number1;
    int number2;
    int number3;
    int sum;
     
    Scanner scan=new Scanner(System.in);
     
    System.out.println("Please enter 1st number: ");
    number1 = scan.nextInt();
     
    System.out.println("Please enter 2nd number: ");
    number2 = scan.nextInt();
     
    System.out.println("Divide the sum of the two numbers by: ");
    number3 = scan.nextInt();
     
    sum = (number1 + number2)/ number3;
     
    return sum;
     
    }
     
     
    public String display(){
     
    System.out.println("****************************** ***");
    System.out.println("*                                                         *");
    System.out.println("*        This is a simple program                 *");
    System.out.println("*      that performs a mathematical           *");
    System.out.println("*         calculation. The answer is:            *");
    System.out.println("*                    "+ sum +"                        *");
    System.out.println("*                                                         *");
    System.out.println("****************************** ***");
     
    }
    }


    basically my instructor wants me to make a program. his guidelines is this.

    You are required to write a command line program in Java that declares three numbers.
    Write a method called calc() that takes in the three numbers as parameters. Inside this
    method, you must add the first two numbers together and divide the answer by the
    third. The value of the sum must be returned. Write another method called display()
    that prints the following to the screen:
    ******************************************
    * *
    * This is a simple program *
    * that performs a mathematical *
    * calculation. The answer is: *
    * [answer] *
    ******************************************

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can you guys help me fix my assignment

    You missed responding to this:
    Can you explain what the code does now, post its output and add comments showing what is wrong with the output and show what you want the output to look like?

    If you are getting errors, copy and paste them here.


    Your code is not properly formatted making it hard to read and understand. The statements should not all start in the first column.
    They should be indented to show the nesting logic.
    Last edited by Norm; May 28th, 2012 at 05:00 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, that seems like a pretty straight forward assignment... What I would do:

    1. Write a method that reads that users input;
    2. Write a method that does that calculation, per the requirements;
    3. Write a method that displays the result.

    In the main method would call the methods in the order above... Done deal. The solution described would have less lines than what it took to write what I would do, but I am not a believer of giving the answers, but direction..

    If you have specific questions, let me know, but to tell you the answer 100%, doesn't help you outside of satisfying your assignment, which is not the reason for taking thr class in the first place...

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Can you guys help me fix my assignment

    For starters, your calc method is supposed to allow three parameters into it, which it doesn't how it is written. Your instructor wants you to pass the three numbers into your calc method, not have your calc method do everything. It should look something like this.

    public int calc(int num1, int num2, int num3) {
         //Do your calculations and return the result
    }

    You will also have to provide a main method to actually be able to call these methods and have it run in the console. Hope this helps.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, I would actually pass floats (and return) instead of ints, unless you need even further precision, where doubles would be used. But again three methods would be written in my solution.

Similar Threads

  1. HI guys
    By nchmurali in forum Member Introductions
    Replies: 2
    Last Post: December 15th, 2011, 02:43 AM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Hi guys
    By nextngema in forum Java SE APIs
    Replies: 2
    Last Post: July 17th, 2010, 04:34 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  5. need help in my assignment guys :(
    By Mr.cool in forum Collections and Generics
    Replies: 7
    Last Post: December 28th, 2009, 08:30 AM