Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2005, 7:07 PM   #1
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
Quick java program

This is a program that I wrote for my cs101 class. I had help with it because I've never written in java before.

/*
 * Assignment.java
 *
 * Created on November 10, 2005, 1:00 PM
 *
 * This program was writen for an assignment for my CS 101 class.
 * Assignment asks the user for a whole number and returns the whole
 * plus each value between the number given and 0 (for example: the
 * user enters in '3', this program takes three and adds ((((3)+ 2)+ 1)+ 0)).
 *
 */

/**
 *
 * author Alvin D. Morris
 * Credit also to my friend Kurt who helped me with my java noobie-ness.
 *
 */
import java.util.*;

public class Assignment
{
	// Method.
	public static int yeah(int y)
	{
		int counter = 0;
		// tv = total value.
		int tv = 0;

		// while loop adds the counter to the total value and then adds one to the
		// counter until the counter is >= y.
		while(counter <= y)
		{
			tv = tv + counter;
			counter++;
		}
		return tv;
	}
	// Main body of Program.
	public static void main(String[] args)
	{
		Scanner kb = new Scanner(System.in);
		int x;
		int y = 0;

		// do loop does everything in the loop at least once.
		do
        {
			System.out.println("Type in a whole number (negative to quit): ");
       		x = kb.nextInt();
			// if statement.
			if(x >= 0)
			{
				y = yeah(x);
				System.out.println(y);
			}
		// while loop.
		}while (x >= 0);
	}
}

any critiques or comments welcome... I'm mainly looking for critiques
Yarvin is offline   Reply With Quote
Old Nov 20th, 2005, 8:08 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
My only critique is I think instead of a while loop inside of the yea(int) function a for loop would have been better...

public static int yea(int y) {
    for(int counter=0,tv=0;counter<=y;counter++)
        tv += counter;
}
__________________

tempest is offline   Reply With Quote
Old Nov 20th, 2005, 8:19 PM   #3
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
Quote:
Originally Posted by Yarvin
This is a program that I wrote for my cs101 class. I had help with it because I've never written in java before.

/*
 * Assignment.java
 *
 * Created on November 10, 2005, 1:00 PM
 *
 * This program was writen for an assignment for my CS 101 class.
 * Assignment asks the user for a whole number and returns the whole
 * plus each value between the number given and 0 (for example: the
 * user enters in '3', this program takes three and adds ((((3)+ 2)+ 1)+ 0)).
 *
 */

/**
 *
 * author Alvin D. Morris
 * Credit also to my friend Kurt who helped me with my java noobie-ness.
 *
 */
import java.util.*;

public class Assignment
{
	// Method.
	public static int yeah(int y)
	{
		int counter = 0;
		// tv = total value.
		int tv = 0;

		// while loop adds the counter to the total value and then adds one to the
		// counter until the counter is >= y.
		while(counter <= y)
		{
			tv = tv + counter;
			counter++;
		}
		return tv;
	}
	// Main body of Program.
	public static void main(String[] args)
	{
		Scanner kb = new Scanner(System.in);
		int x;
		int y = 0;

		// do loop does everything in the loop at least once.
		do
        {
			System.out.println("Type in a whole number (negative to quit): ");
       		x = kb.nextInt();
			// if statement.
			if(x >= 0)
			{
				y = yeah(x);
				System.out.println(y);
			}
		// while loop.
		}while (x >= 0);
	}
}

any critiques or comments welcome... I'm mainly looking for critiques
To start, you can lose almost all the comments you have. Especially the do while loop you labeled a while loop. A comment to tell you that the next line is an if statement puts new meaning to the word useless.
		// tv = total value.
		int tv = 0;
it's good that you commented that, but what would be better is if you just called the variable "total" in the first place.

Coding is not so much that your code is functional, but is also about the comments being useful, style, and so on. Otherwise, good job.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Nov 20th, 2005, 8:21 PM   #4
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
Quote:
Originally Posted by tempest
My only critique is I think instead of a while loop inside of the yea(int) function a for loop would have been better...

public static int yea(int y) {
    for(int counter=0,tv=0;counter<=y;counter++)
        tv += counter;
}
thx for the feedback....

I tried replacing the while loop with the for loop like in your example but I got a bunch of errors and I didn't quite understand the errors because when I went back to fix the errors I couldn't find anything wrong....

but I will keep this in mind for my next program.... Thanks
Yarvin is offline   Reply With Quote
Old Nov 20th, 2005, 8:23 PM   #5
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
Quote:
Originally Posted by 2roll4life7
To start, you can lose almost all the comments you have. Especially the do while loop you labeled a while loop. A comment to tell you that the next line is an if statement puts new meaning to the word useless.
		// tv = total value.
		int tv = 0;
it's good that you commented that, but what would be better is if you just called the variable "total" in the first place.

Coding is not so much that your code is functional, but is also about the comments being useful, style, and so on. Otherwise, good job.

good point... thank you. I will change it to total instead of total value
Yarvin is offline   Reply With Quote
Old Nov 20th, 2005, 8:36 PM   #6
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Quote:
Originally Posted by Yarvin
thx for the feedback....

I tried replacing the while loop with the for loop like in your example but I got a bunch of errors and I didn't quite understand the errors because when I went back to fix the errors I couldn't find anything wrong....

but I will keep this in mind for my next program.... Thanks
My bad, apparently variable scope is different then i expected, and I also forgot to return something...

[php]
import java.io.*;

public class test {
public static void main(String args[]) {
System.out.println(yeah(3));
}

public static int yeah(int y) {
int tv=0;
for(int counter=0;counter<=y;counter++)
tv += counter;

return tv;
}
}
[/php]
__________________

tempest is offline   Reply With Quote
Old Nov 20th, 2005, 9:56 PM   #7
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
haha.... thx.... I'll keep this in mind.....

I know about the java.util but whats the java.io?
Yarvin is offline   Reply With Quote
Old Nov 20th, 2005, 11:04 PM   #8
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Guess
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 29th, 2005, 4:22 PM   #9
ersken
Newbie
 
Join Date: Oct 2005
Posts: 2
Rep Power: 0 ersken is on a distinguished road
Recursion is beautiful.
	public static int sum(int n) { 
		return (n > 0) ? n + sum(n-1) : 0;   
	}
__________________
Segmentation fault (core dumped)
ersken is offline   Reply With Quote
Old Nov 29th, 2005, 6:16 PM   #10
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
seems pointless ...
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC