| View previous topic :: View next topic |
| Author |
Message |
tylerxian How do I cheat?
Reputation: 0
Joined: 18 Jan 2009 Posts: 4
|
Posted: Wed Mar 18, 2009 10:37 pm Post subject: Java Problem |
|
|
Hello, sorry to impose on your time but can anyone help me write a short program from Java? I need to turn this program in to my teacher soon, but I don't really get Java.
I'm supposed to write a program that sums up all the integers between two numbers. Something like:
Enter starting number:20
Enter ending number:40
The sum from (starting number) to (ending number) is (the sums added in between ex. 20 +21 + 22... + 40)
Thank you.
|
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Wed Mar 18, 2009 11:02 pm Post subject: |
|
|
If you know a bit of java, you can turn this into a working program pretty easily.
| Code: | int num = 20;
int i = 0;
int total = 0;
list<int> numToAdd = new list<int>();
static void Main(string[] args)
{
for(i = 0;i < 20; i++;)
{
int temp = i + 20;
numToAdd.Add(temp);
}
foreach(int n in numToAdd)
{
total += n;
}
System.Out.PrintIn(total.ToString());
} |
Never used java lists, might have to fix that up.
If it compiles after pasting, its a miracle!
Oh yes, you'll have to accept input, or if your afraid to do that, post back and I'll get it to accept command line arguments for the numbers.
|
|
| Back to top |
|
 |
sloppy Expert Cheater
Reputation: 0
Joined: 17 Aug 2008 Posts: 123
|
Posted: Wed Mar 18, 2009 11:04 pm Post subject: |
|
|
| Code: | int start = 20, end = 40, sum = 0;
for (int i = start; i <= end; i++)
{
sum += i;
} |
Been awhile since I've played with java, but the generic code above should do?
edit:
damn you frostbyt3 *shakes fist*
|
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Thu Mar 19, 2009 7:59 am Post subject: |
|
|
int start = 20;
int end = 40;
//Using a arithmatic series forumla
double value = (end - start)((start + end)/2.0)
Edit: make a small mistake, here's the whole class
| Code: | //Using a arithmetic series formula
class test
{
public static void main (String[] args)
{
int start = 1;
int end = 5;
double value = ((end - start + 1) / 2.0) * (start + end);
System.out.println (value);
}
} |
Edit 2:
Yo let you know, the arithmetic series formula allows you to find the sum of a series of numbers.
Sn = (n/2)(N1 + Nn)
where Sn is the sum
where n is the number of terms
where N1 is the first number in the series
where Nn is the last number in the series.
_________________
|
|
| Back to top |
|
 |
NoManchesPuto I post too much
Reputation: 0
Joined: 24 Jan 2009 Posts: 2820
|
Posted: Thu Mar 19, 2009 3:19 pm Post subject: java |
|
|
| Sorry to bother yall, but can someone give me a download link for java so i may create projects and stuff?? plz and thx rep if it works.
|
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Thu Mar 19, 2009 3:41 pm Post subject: Re: java |
|
|
| Lord Dread wrote: | | Sorry to bother yall, but can someone give me a download link for java so i may create projects and stuff?? plz and thx rep if it works. |
downlaod eclipse's IDE. I find that it is very good.
DO NOT go for Ready To Program, as it has incomplete libraries and is crap. After all, they did go out of buisness.
_________________
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Thu Mar 19, 2009 4:52 pm Post subject: Re: java |
|
|
| Code: |
import java.util.Scanner;
public class JHomeWork {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int min = 0, max = 0;
try{
System.out.print("Min. number: ");
min = get.nextInt();
System.out.print("Max. number: ");
max = get.nextInt();
}
catch(Exception exc){
System.out.printf("Excepcion: %s\n",exc.toString());
}
finally{
for(int i = min; i != max+1; i++)
System.out.print(i + " - ");
}
}
} |
| kitterz wrote: | | Lord Dread wrote: | | Sorry to bother yall, but can someone give me a download link for java so i may create projects and stuff?? plz and thx rep if it works. |
downlaod eclipse's IDE. I find that it is very good.
DO NOT go for Ready To Program, as it has incomplete libraries and is crap. After all, they did go out of buisness. |
NetBeans is much better.
| Frostbyt3 wrote: | If you know a bit of java, you can turn this into a working program pretty easily.
| Code: | int num = 20;
int i = 0;
int total = 0;
list<int> numToAdd = new list<int>();
static void Main(string[] args)
{
for(i = 0;i < 20; i++;)
{
int temp = i + 20;
numToAdd.Add(temp);
}
foreach(int n in numToAdd)
{
total += n;
}
System.Out.PrintIn(total.ToString());
} |
Never used java lists, might have to fix that up.
If it compiles after pasting, its a miracle!
Oh yes, you'll have to accept input, or if your afraid to do that, post back and I'll get it to accept command line arguments for the numbers. |
Java is OOP... so if we want to work with java, lets work with OOP style, otherwise, use a structured programming. You code is C with Java stuff
Last edited by igoticecream on Thu Mar 19, 2009 4:59 pm; edited 1 time in total |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Mar 19, 2009 4:58 pm Post subject: Re: java |
|
|
| igoticecream wrote: | | kitterz wrote: | | downlaod eclipse's IDE. I find that it is very good. | NetBeans is much better. | They're both awful. Just get a good text editor and the compiler from Sun's site. For example, Notepad++ and JDK = way better than those IDEs.
|
|
| Back to top |
|
 |
tylerxian How do I cheat?
Reputation: 0
Joined: 18 Jan 2009 Posts: 4
|
Posted: Thu Mar 19, 2009 5:05 pm Post subject: |
|
|
| I really do appreciate the help! Thanks a lot guys!
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Thu Mar 19, 2009 5:09 pm Post subject: Re: java |
|
|
| Jani wrote: | | igoticecream wrote: | | kitterz wrote: | | downlaod eclipse's IDE. I find that it is very good. | NetBeans is much better. | They're both awful. Just get a good text editor and the compiler from Sun's site. For example, Notepad++ and JDK = way better than those IDEs. |
Just no
You cannot compare developing with an IDE with delevoping with a text editor =S
Just a question: What do you do when your application do not work as you want? Do you debug? Then... How?
|
|
| Back to top |
|
 |
tylerxian How do I cheat?
Reputation: 0
Joined: 18 Jan 2009 Posts: 4
|
Posted: Thu Mar 19, 2009 5:37 pm Post subject: |
|
|
Sorry to bother you again, but there's a misunderstanding regarding the program, igoticecream's program is the closest to the output that I wanted. Maybe a better example would be better.
Ex.
Starting number: 20
Ending number: 40
The sum of the integers from 20 to 40 is 630.
I was amazed by the programs though, I could only read about 60% of it, maybe even less. (I might take more Java next year, I'm just a BASIC student right now.)
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Thu Mar 19, 2009 6:36 pm Post subject: |
|
|
| Code: |
import java.util.Scanner;
public class JHomeWork {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int min = 0, max = 0, holder = 0;
try{
System.out.print("Min. number: ");
min = get.nextInt();
System.out.print("Max. number: ");
max = get.nextInt();
}
catch(Exception exc){
System.out.printf("Excepcion: %s\n",exc.toString());
}
finally{
for(int i = min; i != max+1; i++){
holder += i;
}
System.out.print(holder);
}
}
}
|
|
|
| Back to top |
|
 |
tylerxian How do I cheat?
Reputation: 0
Joined: 18 Jan 2009 Posts: 4
|
Posted: Fri Mar 20, 2009 12:25 am Post subject: |
|
|
Thanks a lot for your help igoticecream! I'm such a beginner at Java, the best 'error-free' code I could come up with was
| Code: |
import apcslib.*;
import chn.util.*;
public class 1JAVA31
{
public static void main(String[] args)
{
ConsoleIO c = new ConsoleIO();
System.out.print("Enter the starting number:");
int sn = c.readInt();
System.out.print("Enter the ending number:");
int en = c.readInt();
int rn = sn + en;
System.out.println("The running sum between " + sn);
System.out.println("and " + en);
System.out.println("is" + rn);
}
}
|
Then, I was stumbled on what to do next, I guess that's what you get for taking only a few lessons on Java and the rest on BASIC. Anyways thanks a lot!
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Mar 20, 2009 3:57 am Post subject: Re: java |
|
|
| igoticecream wrote: | Just no
You cannot compare developing with an IDE with delevoping with a text editor =S | I was saying they're both awful IDEs.
| igoticecream wrote: | | Just a question: What do you do when your application do not work as you want? Do you debug? Then... How? | jdb. How do you think ppl with for example gcc debug? You know, you can debug on command line too... It doesn't require an IDE with GUI.
|
|
| Back to top |
|
 |
VolatileAce Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Sat Mar 21, 2009 8:56 pm Post subject: |
|
|
Another way to write it to run at O(1) time instead of O(n) time from looping through all the numbers would be something like this, notice I do not handle moronic errors that may be generated by the user:
| Code: | /**
* @(#)Tylerxian.java
*
*
* @author VolatileAce
* @version 1.00 2009/3/21
*/
import java.util.Scanner;
public class Tylerxian{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int x = 0, y = 0;
System.out.print("Please enter the starting number: ");
x = in.nextInt();
System.out.print("Please enter the ending number: ");
y = in.nextInt();
assert(x < y);
System.out.print("The sum of all the numbers inbetween(inclusive) is: " +
((y-x>>1<<1==y-x)?(y-x+1)*(x+y>>1):(y-x+1)*(x+y>>1)+((y+1-x)*(x+1+y>>1)-(y+1-x)*(x+y>>1)>>1)));
}
} |
What is being done here is that instead of adding up the number, I find the number in the middle of min and max by adding min with the subtraction of max and min. Then I multiply that number by the difference between min and max. Bitwise operations are used here to further increase efficiency of the code.
Hope this helps!
Last edited by VolatileAce on Sat Mar 21, 2009 10:44 pm; edited 1 time in total |
|
| Back to top |
|
 |
|