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! :)
Re: Please can you help with my program? How to make it
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?
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
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.
Re: Please can you help with my program? How to make it
Code java:
<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);
>
1 Attachment(s)
Re: Please can you help with my program? How to make it
for eAttachment 1577
For all the errors i get the same problem:
class, interface, enum expected
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.
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.
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.
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?
Code java:
<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);
}
}
}>
Code java:
<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();
}
}
}
}>
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.
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.
Re: Please can you help with my program? How to make it
like this?
Code java:
<
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);
}
}
}
>
Code java:
<
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
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.