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 4 of 4

Thread: someone please be kind enough demonstrate a code like this......

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default someone please be kind enough demonstrate a code like this......

    This is the work

    - From the main method call a method named sumOfDigits(), without passing any parameters
    - Create a method named sumOfDigits()
    - In the sumOfDigits() method declare the variable N of type int
    - Assign the value 100 to the variable N.
    - Using the sum of digits formula: (1 + N)*(N/2)
    print the total amount of the sum of digits from 1 to 100

    __________________________________________________ __________________________

    ive tried using

    public void sumOfNumbers() {

    formula = (1+100)*(2/100)
    }


    then I do system print (formula) but it keeps displaying 0

    ive been messing around with it also. this is my code so far


    __________________________________________________ _________



    package Lunix;


    public class chaplew {

    private String myName;
    private double creditsTaken;
    private double totalCredits;
    private double GPA;
    private String major;
    private String className;
    private int maxValue;
    private double N;





    public chaplew(String myName, double creditsTaken,
    double totalCredits, double GPA, String major, String className, int maxValue, int N )
    {
    this.myName = myName;
    this.GPA = GPA;
    this.className = className;
    this.major = major;
    this.totalCredits = totalCredits;
    this.creditsTaken = creditsTaken;
    this.maxValue = maxValue;
    this.N = N;


    }







    public String getmyName()
    {
    return myName;
    }
    public double getcreditsTaken()
    {
    return creditsTaken;
    }
    public double gettotalCredits()
    {
    return totalCredits;
    }
    public double gteGPA()
    {
    return GPA;
    }
    public String getmajor()
    {
    return major;
    }
    public String getclassName()
    {
    return className;
    }




    public void addNumbers(int addition)
    {
    maxValue = addition+1;

    }


    public void subtractNumbers(int subtraction)
    {
    maxValue = subtraction-1;
    }

    public void multiplyNumbers(int multiply)
    {
    maxValue = multiply*1;
    }

    public void divideNumbers(int divide)
    {
    maxValue = divide/1;
    }

    public void modNumbers(int mod)
    {
    maxValue = mod%1;
    }





    public void sumOfNumbers(int form)
    {
    N = form;
    }















    public static void main(String[] args) {
    // TODO code application logic here



    chaplew p = new chaplew("William", 9, 70, 3.1, "Information Technology", "Operating Systems", 100, 1);
    System.out.println("my name is...." + p.myName);
    System.out.println("credits I am enrolled is..." + p.creditsTaken);
    System.out.println("I have taken this amount of credits throughout college..." + p.totalCredits);
    System.out.println("My gpa is..." + p.GPA);
    System.out.println("My Major is.." + p.major);
    System.out.println("This class is called......." + p.className);
    System.out.println();




    p.addNumbers(100);
    System.out.println("Max Value 1 + 2 + 3 + 4 + 5");
    System.out.println(p.maxValue);
    System.out.println(p.maxValue+(1));
    System.out.println(p.maxValue+(2));
    System.out.println(p.maxValue+(3));
    System.out.println(p.maxValue+(4));


    p.subtractNumbers(100);
    System.out.println();
    System.out.println("Max Value 1 - 2 - 3 - 4 - 5");
    System.out.println(p.maxValue-1);
    System.out.println(p.maxValue-2);
    System.out.println(p.maxValue-3);
    System.out.println(p.maxValue-4);
    System.out.println(p.maxValue-5);


    p.multiplyNumbers(100);
    System.out.println();
    System.out.println("Max Value 1 * 2 * 3 * 4 * 5");
    System.out.println(p.maxValue*1);
    System.out.println(p.maxValue*2);
    System.out.println(p.maxValue*3);
    System.out.println(p.maxValue*4);
    System.out.println(p.maxValue*5);



    p.divideNumbers(100);
    System.out.println();
    System.out.println("Max Value 1 / 2 / 3 / 4 / 5");
    System.out.println(p.maxValue/1);
    System.out.println(p.maxValue/2);
    System.out.println(p.maxValue/3);
    System.out.println(p.maxValue/4);
    System.out.println(p.maxValue/5);

    p.modNumbers(100);
    System.out.println();
    System.out.println("Max Value 1 % 2 % 3 % 4 % 5");
    System.out.println(p.maxValue%1);
    System.out.println(p.maxValue%2);
    System.out.println(p.maxValue%3);
    System.out.println(p.maxValue%4);
    System.out.println(p.maxValue%5);






    System.out.println();
    p.addNumbers((1+100)*(2/100));
    System.out.println((1+100)*(2/100));
























    }
    }


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

    Default Re: someone please be kind enough demonstrate a code like this......

    What is your question?

    do system print (formula) but it keeps displaying 0
    Where does the code change the value so it would not be 0?

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

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: someone please be kind enough demonstrate a code like this......

    Quote Originally Posted by willc86 View Post
    ...
    - Using the sum of digits formula: (1 + N)*(N/2)...
    First of all, that is not the formula for a sum of digits. It is the formula to calculate the sum of the first N positive integers.

    Secondly, and more importantly, I can't see where you have written code that implements the given formula. In fact, I can't see in your post where you have done a single thing that is specified by the assignment.

    Why (oh, why) did you post all of the other stuff?

    Bottom line: Read the assignment. Follow it exactly. If you don't get the right answer (or if the program doesn't compile for some reason), post your entire code with stuff needed for the part of the assignment that you don't understand.


    Explain what you don't understand about what happens when you compile or run the code.



    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: someone please be kind enough demonstrate a code like this......

    Hi, the formula is ((N+1)*N)/2. In this first the jvm calculate the (N+1) ,after it multiply the result of (N+1) with N and it perform the division opertation on result (N+1)*N with 2.

    hence formula is

    formula = ((1+100)*100)/2;

Similar Threads

  1. Am I doing this right? (demonstrate TWO sorting algorithms)
    By Diizzle in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2012, 01:22 AM
  2. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  3. [SOLVED] What kind of object instantiation is this?
    By Lord Voldemort in forum Object Oriented Programming
    Replies: 3
    Last Post: July 7th, 2011, 07:00 AM
  4. sync two diffrent kind of threads?
    By adamruss in forum Threads
    Replies: 1
    Last Post: January 10th, 2010, 08:59 PM
  5. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM