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

Thread: Please can you help with my program? How to make it

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Please can you help with my program? How to make it

    hi please i do not understand how to make a program for the following questions, im a beginner,
    For the first one i put it in but it does not work:
    I use netbeans to write the program
    The questions can be found in Java An Introduction to problem solving & Programming, Sixth edition

    Convert the following code so that it uses nested while statements instead of for statements:
    int s = 0;
    int t = 1;
    for (int i = 0; i < 10; i++) {
    s = s + i;
    for (int j = i; j > 0; j−−) {
    t = t * (j - i); }
    s = s * t;
    System.out.println("T is " + t); }
    System.out.println("S is " + s);


    Question number 14:
    π =pi


    Suppose that your work for a beverage company. The Company wants to know the optimal cost for a cylindrical container that holds a specified volume. Write a fragment of code that uses and ask-before-iteration loop. During each iteration of the loop, your code will ask the user to enter the volume and the radius of the cylinder. Compute and display the height and cost of the container. Use the following formulas, where V is the volume, r is the radius, h is the height, and C is the cost.
    h=V /(π r2)
    C = 2π r (r + h)





    Please & Thank you so much!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    it does not work:
    If there are errors please copy and paste the full text of the error messages.
    Otherwise please explain what "does not work" means? If its output is wrong, paste its output here and explain what is wrong with it.

    What have you coded for the two questions you posted?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    heythisgreg (December 1st, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    Hello, this is what i have for the firs problem:
    i get class, interface, or enum expected: all ones that have a * have a problem, i know it has to do with the "int" but i dont know how to fix it.

    package q21;
    import java.util.Scanner;

    public class Q21 {
    Scanner kB = new Scanner(System.in);
    public static void main(String[] args) {
    }
    }
    *int s = 0;
    *int t = 1;
    *while (int i = 0; i <10; i++)
    {
    s = s +i;
    * do (int j = i; j>0;j--)
    {
    t=t * (j - i);
    * }
    s = s * t;
    * System.out.println ("T is" + t);
    *}
    System.out.println ("S is" + s);



    As for the second problem i dont know how to start it

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Please copy the full text of the error messages and paste them here so we can see where and what the problems are.

    It looks like the code with the * is outside the {}s for a method. Move the } so the code is inside the method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    <package q21;
    import java.util.Scanner;
     
    public class Q21 {
        Scanner kB = new Scanner(System.in);
        public static void main(String[] args) {
        }
    }
    int s = 0;
    int t = 1;
    while (int i = 0; i <10; i++)
    {
            s = s +i;
            do (int j = i; j>0;j--)
                    {
                            t=t * (j - i);
                    }
            s = s * t;
            System.out.println ("T is" + t);
    }
    System.out.println ("S is" + s);
    >

  7. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    for e13.jpg


    For all the errors i get the same problem:
    class, interface, enum expected

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    You need to check the positions of the }s for the methods to make sure they enclose the code.

    The formatting needs to be corrected. Statements inside of {}s should be indented 3-4 spaces.

    There are too many statements starting in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    heythisgreg (December 1st, 2012)

  10. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    okay thanks i got it, and for the next one? i dont know where i can to get help on it.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    What are the steps the program needs to do for the next one? Make a list of simple steps (pseudo code) that describes what needs to be done. When you get the logic worked out, then code it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    heythisgreg (December 1st, 2012)

  13. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    okay so i got the problem, and it does not show any errors for both of the questions, however, when i press run it says build successful and nothing comes out. I am not sure what to do, there must be something wrong with it still, can you try to see if u can run it?

    <package q21;
    import java.util.Scanner;
    public class Q21 {
        Scanner kB = new Scanner(System.in);
        public static void main(String[] args) {
        }
    int s = 0; 
    int t = 1; 
    int i = 0; 
    {
    while (i < 10)
    { 
    s = s + i; 
    int j = i; 
    while(j > 0) 
    { 
    t = t * (j - i); 
    j--; 
    } 
    s = s * t; 
    System.out.println("T is " + t); 
    i++;  
    System.out.println("S is " + s);
    }
    }
    }>


    <package q22;
    import java.util.Scanner;
    public class Q22 {
        Scanner keyboard = new Scanner(System.in); 
        public static void main(String[] args) {
    }
    {   
    System.out.println("Enter cylinder volume or -1 to exit:"); 
    double v = keyboard.nextDouble(); 
    double r = 0.0; 
    if(v > 0.0) 
    { 
    System.out.println("Enter cylinder radius:"); 
    r = keyboard.nextDouble(); 
    } 
    while(v > 0.0) 
    { 
    double h = v / (Math.PI * (r * r)); 
    double c = 2 * Math.PI * r * (r + h); 
    System.out.println("Height: " + h + "\nCost: " + c); 
    System.out.println("Enter cylinder volume or -1 to exit:"); 
    v = keyboard.nextDouble(); 
    if(v > 0.0) 
    { 
    System.out.println("Enter cylinder radius:"); 
    r = keyboard.nextDouble(); 
    } 
    }
    }
    }>

  14. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    Please edit your coded and give it proper indentations. Indent 2-3 spaces inside each pair of {}s
    Unformatted code is hard to read and understand.
    nothing comes out
    Does the program end or does it run forever? Add some println statements inside the loops that print out the value of the variable used in the condition that will stop the looping.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please can you help with my program? How to make it

    like this?



    <
    package q21;
    import java.util.Scanner;
    public class Q21 {
        Scanner kB = new Scanner(System.in);
        public static void main(String[] args) {}
        int s = 0; 
        int t = 1; 
        int i = 0; 
    {
            while (i < 10)
    { 
                    s = s + i; 
                    int j = i; 
            while(j > 0) 
    { 
                    t = t * (j - i); 
    j               --; 
    } 
                    s = s * t; 
     
    System.out.println("T is " + t); 
    i++;  
     
    System.out.println("S is " + s);
     
    }
    }
    }
    >

    <
    package q22;
    import java.util.Scanner;
    public class Q22 {
        Scanner keyboard = new Scanner(System.in); 
        public static void main(String[] args) {
    }
     
    {   
            System.out.println("Enter cylinder volume or -1 to exit:"); 
            double v = keyboard.nextDouble(); 
            double r = 0.0; 
        if(v > 0.0) 
    { 
            System.out.println("Enter cylinder radius:"); 
            r = keyboard.nextDouble(); 
    } 
        while(v > 0.0) 
    { 
            double h = v / (Math.PI * (r * r)); 
            double c = 2 * Math.PI * r * (r + h); 
     
    System.out.println("Height: " + h + "\nCost: " + c); 
    System.out.println("Enter cylinder volume or -1 to exit:"); 
             v = keyboard.nextDouble(); 
        if(v > 0.0) 
    {
     
    System.out.println("Enter cylinder radius:"); 
    r = keyboard.nextDouble(); 
    } 
    }
    }
    }
    >


    --- Update ---

    well wht happens when i run it it says:
    run:
    BUILD SUCCESSFUL (total time: 0 seconds)



    that's all

  16. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Please can you help with my program? How to make it

    You still need to format the code. The { and }s should not all be in the first column.

    Check the locations of the {}s you have too many/
    No } should be on a line with anything else. Put all }s on their own line.

    There are two programs. Which are you executing?
    You need some more printlns inside all the while loops that print out the values of the variables used:
    i and j are what control the loops. You need to see them before the while statements and inside the loops. If nothing prints then you need to add more in different places outside and inside the loops.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. can anyone help me make this program
    By amin45 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 16th, 2012, 07:41 PM
  2. How to make program ask for options again
    By m7abraham in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 25th, 2012, 11:12 PM
  3. I am trying to make a messenger program!
    By piulitza in forum Java Networking
    Replies: 13
    Last Post: December 21st, 2011, 01:38 PM
  4. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  5. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM