since Jul 10, 2011

Cows and Likes

Cows

Total received

0

In last 30 days

0

Total given

0

Likes

Total received

0

Received in last 30 days

0

Total given

2

Given in last 30 days

0

Forums and Threads

Scavenger Hunt

Ranch Hand Scavenger Hunt

Greenhorn Scavenger Hunt

I am still in the beginning stages of learning Java. I have a friend at work "math challenged" so I made this very basic little program and I would like to "export" the code so I can put on her computer and she can "run" it. Is there a simple way to do this? I tried to export an executable jar file from Eclipse but when I double click on it I get an error. Any help will be appreciated.

Here is the code for my program:

Here is the code for my program:

9 years ago

Checkerboard is supposed to be centered with red and black checkers set up for play.

Do you want to tear apart my work? I will appreciate it! As you will see, I haven't figured out how to center the pieces yet.

Do you want to tear apart my work? I will appreciate it! As you will see, I haven't figured out how to center the pieces yet.

10 years ago

This works but I am sure not what the instructor wanted or was expecting, but if the user inputs number 4 as in the exercise question, the program does generate the first 4 odd integers and adds up their combined total. I don't like how I initialized x as -1 to make this work. I really wanted to use a boolean expression to only generate odd numbers but I couldn't get it to work. If anyone wants to re write this in a better manner, that will be helpful.

Here is my latest code:

Here is my latest code:

10 years ago

Update: I have made some progress in my opinion. I am able to take in an integer and generate a list of integers starting at 1 up to the amount input. So if the user inputs "5", the output will be 1 2 3 4 5 and will add up the total of all of those together. 1+2+3+4+5 = 15. Now I just need to have the numbers generated only be odd, so 1 3 5 7 9 if 5 was input. Here is my program method:

I tried to add to the while statement: while ((getInt < x) && (getInt % 2 != 0)) { but that didn't work. I'm not giving up yet. Again, any guidance will be appreciated.

I tried to add to the while statement: while ((getInt < x) && (getInt % 2 != 0)) { but that didn't work. I'm not giving up yet. Again, any guidance will be appreciated.

10 years ago

I guess I put the cart before the horse. Before knowing how to calculate the total of a specific number of integers I need to know how can I generate a specific amount of integers? Once I can create them I will worry about adding them. So what I have tried so far is:

int x = readInt("Enter a number: ");

and then that number input will tell the program how many integers to create.

For example, if the user inputs 4, the program will generate four digits, like 1 2 3 4.

I created a for statement:

for (int i = 1; i < x; i+=2);

Which will give me only odd numbers, but doesn't give me the four integers. So I don't believe the for statement is the correct approach. I feel like generating a set number of integers starting at 1 and increasing by two a certain amount of times, must be simple, but for some reason I'm stuck.

Any more help appreciated.

int x = readInt("Enter a number: ");

and then that number input will tell the program how many integers to create.

For example, if the user inputs 4, the program will generate four digits, like 1 2 3 4.

I created a for statement:

for (int i = 1; i < x; i+=2);

Which will give me only odd numbers, but doesn't give me the four integers. So I don't believe the for statement is the correct approach. I feel like generating a set number of integers starting at 1 and increasing by two a certain amount of times, must be simple, but for some reason I'm stuck.

Any more help appreciated.

10 years ago

I completed the exercise. Thank you to everyone who gave input, especially Fred. I added a couple of lines at the end just to verify the program was running correctly. I realized I had to put the counter after break because if not it would count the input of the sentinel. I'm sure this could be refined and improved but it works. Thank you all again.

10 years ago

fred rosenberger wrote:there is nothing fancy about it...it would just be a variable you create. just like you have your "double average = 0", you would set up a "int numInputs = 0".

Then each time you get a new value input, you add 1 to it. there are several ways of doing that:

numInputs = numInputs + 1;

or

numInputs += 1;

in fact, adding 1 to an int is so common, there is a special shortcut:

numInputs++;

As soon as I get home from work I will work with this and report back. Thank you very much for your patience and guidance!

10 years ago

fred rosenberger wrote:

Darrin Altman wrote:I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.

So how do you count how many numbers are added? each time you hear a new value from me, you increase your count by 1.

So you need to keep track of two things...the running total of all the input numbers, and a count of how many numbers you've gotten.

I really don't believe up to this point the book has shown how to set up an input counter. I will continue to work on this and report back.

10 years ago

fred rosenberger wrote:the first thing you should always do is work out how you'd do something by hand - i.e. using paper and pencil.

so, assume I was going to read a list of numbers, and I want you to tell me the average of them once I tell you i'm done. How would you do that?

I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.

10 years ago

I sure hope I don't wear out my welcome. I am struggling to get my head around these programming exercises.

"Write a program that reads in a positive integer N and then calculates and displays the sum of the first N integers. For example if N is 4, your answer should be 16, 1+3+5+7."

After hours of trying to get this, I decided to just try and display the digits but I haven't been able to do that just yet. Here is what I have so far which only does a countdown of the odd integers < N. Can someone set me on a better path? Thank you for your patience and help.

"Write a program that reads in a positive integer N and then calculates and displays the sum of the first N integers. For example if N is 4, your answer should be 16, 1+3+5+7."

After hours of trying to get this, I decided to just try and display the digits but I haven't been able to do that just yet. Here is what I have so far which only does a countdown of the odd integers < N. Can someone set me on a better path? Thank you for your patience and help.

10 years ago

Wouter Oet wrote:sumOfNumbers / numberOfNumbers = average

Good luck.

Since the user can input any amount of numbers how can I know the numberOfNumbers or have my program calculate that? If I knew in advance how many they would input then I would, but if not?

10 years ago

Working on a programming question from "The Art and Science of Java". I need to allow the user to input any amount of numbers they want and then when they finish produce the average of their numbers. I wrote a program where I add all of the numbers input but then I'm stuck on the division part because I don't know how many different numbers they input? Any help appreciated. Here is what I have so far:

public void run() {

double average = 0.0;

while (true) {

double score = readDouble("Enter grade: ");

if (score == SENTINEL) break;

average += score / ?;

}

println("The average is: " + average);

}

private static final int SENTINEL = -1;

}

public void run() {

double average = 0.0;

while (true) {

double score = readDouble("Enter grade: ");

if (score == SENTINEL) break;

average += score / ?;

}

println("The average is: " + average);

}

private static final int SENTINEL = -1;

}

10 years ago

shuba gopal wrote:Hi Darrin,

Looks like switch-case will be useful for you.

Have a look at this sample program.http://www.cafeaulait.org/course/week2/42.html

If you are printing upto n, for(int b =0;b<n;b++). Use the switch case inside this for loop

switch(b)

case(1):

break;

case(n):

break;

default:

break;

Thanks

Thank you for your help. I didn't think about using the switch statement inside of the for statement. I will re write the program to make sure I get it. I sure hope I don't wear out my welcome, I seem to have trouble with each programming exercise.

10 years ago

I need to take the song "This old man" and make the necessary changes of the number and rhyming word. I was able to do it but I believe I was supposed to use a for statement or a while statement. Here is what I came up with. Any suggestions on improving this. Thank you for any help.

10 years ago

I am breaking my head trying to figure this out. If you don't have the book, I need to place a tic tac toe board in the center of my window using a single constant called BOARD_SIZE to define the height and width of the figure. I understand how to center an oval or rectangle but not centering the four lines. And the use of the single constant is confusing me also.

Here is the "run portion" (not sure if calling this as "run portion" is correct) of the original program that creates the tic tac toe board, which I am supposed to center and refine. Any help will be greatly appreciated.

add(new GLine(30, 60, 120,60));

add(new GLine(30, 90, 120, 90));

add(new GLine(60, 30, 60, 120));

add(new GLine(90, 30, 90, 120));

Here is the "run portion" (not sure if calling this as "run portion" is correct) of the original program that creates the tic tac toe board, which I am supposed to center and refine. Any help will be greatly appreciated.

add(new GLine(30, 60, 120,60));

add(new GLine(30, 90, 120, 90));

add(new GLine(60, 30, 60, 120));

add(new GLine(90, 30, 90, 120));

10 years ago