![]() |
|
|||||||
| JOIN NOW! | Mark Forums Read |
| Today we are discussing Official Java Thread in the University Forum. Join in the discussion and express your views on Official Java Thread "The thread title says it all really. I'll start off first. Right, so here was the question: Write a program that prints 5 lines showing on each line the number, a colon “:”, the sum ..." |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
The thread title says it all really.
I'll start off first. Right, so here was the question: Write a program that prints 5 lines showing on each line the number, a colon “:”, the sum of all number up to the current number, a comma “,”, and the product of all numbers up to the current number.[size=3] Heres the program: public class ProductNumber { public static void main(String[] args) { int sum = 0; int product = 1; for (int i = 1; i <= 5; i++) { sum += i; // same as using "sum = sum + i;" product *= i; // same as using "product = product * i;" System.out.print(i); System.out.print(": "); System.out.print(sum); System.out.print(" , "); System.out.println(product); } } } Right, now this is the standard form for a java program right? Let me get some things straigh, correct me if I'm wrong. The bit in red...its telling us the name of the program...and the master class...in effect...this is public so that if any other applications would want to use it...it could right? Or am I thinking the wrong way... Ok now the bit in green...(I just explained this...and after a re-read of a chapter...it makes more sense...basically this main method NEEDS to be ALWAYS set out in this way right? Otherwise the program won't run...? The purple is just a simple "for statement"...that speaks for itself... The bit in blue...this is fairly simple...its just declaring variables...but I've seen the programs that have declared them as private OUTSIDE the main method...under the public class....in this program why can't you declare them outside of the class? I'll show you what I mean: class Quadrangle { public float a, b, c, d; public Quadrangle(float a, float b, float c, float d) { this.a = a; this.b = b; this.c = c; this.d = d; } public boolean isSquare() { return a == b && c == d && a == d; } public boolean isRectangle() { return (a == b && c == d) || (a == c && b == d) || (a == d && b == c); } public void setLengths(float a, float b, float c, float d) { this.a = a; this.b = b; this.c = c; this.d = d; } } class QuadrangleMain { public static void main( String[] args) { Right, so the actual program starts running here...in the black-bold text...cos I've noticed it has public static void main... But all that stuff above it.... ![]() The constructor used...I think its "this." However the first statment says "class Quadrangle". Why isnt it public? I thought the Class QuadrangleMain wouldnt be able to use it if its not public?[left]And lastly..I'm guessing that all the variables defined are public...because they need to be accessed by the second class later on in the program...?
Last edited by Zero; 04-11-2008 at 22:28. |
|
|
|
|
|
#3 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,312
My Mood:
Rep Power:
505
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Hey Zero.... Hmm....
The first program has a Main() in a ProductNumber class... All class names are public. It'll be in a package or something that can be 'included' dunno what it's called in Java maybe [Import] and it will be visible to the other programs that want to use it.... Every program that you want to do something on a must have a Main(), your operating system (which is a program itself) will call the Main() as any other method. Void tells the OS to not expect a Return Value from Main(), sometimes you can have a Int Main() which must return a Integer, normally Returned 1 tells the OS that there was a problem with the program... But your OS probably won't do shit anyway..... If you want a program that runs, you need a Main(), you only have one Main method in any program. When you create a executable application out of ProductNumber, and run it, it will look for Main(). You can place a member declaration (or variable declaration) anywhere, but if you place it outside of the Main(), it'll belong to the Class still, but be visible to the whole of Main() and any code branch inside Main(). It'll change the "scope" of the member/variable... The explicit "public" before Main() and the Class name are there just to show that they are not private, if you omit the keyword, they'll be Public by default.... There is no reason why you can't declare variables before Main(). The second program, the Constructor is actually a method that has the same name as the class name.... public Quadrangle(float a, float b, float c, float d) is the Constructor. This. disambiguates the two variables named "a". The This.a will refer to the current instance of Quadrangle's 'a' member variable which is declared in the class definition. And 'a' on it's own will refer to the parameter 'a', which will be the first argument passed to the Constructor method. Basically it tells take the 'a' which is passed in as a argument, and assign it to the member variable a. Class Quadrangle is Public by default if you don't state otherwise. In Java if you don't say it's Private, it will make it Public by default.. And yeah, all the members ar Public, and this time it actually says its public when the variables are defined: class Quadrangle { public float a, b, c, d; Means that they'll be directly accessable when you do: Quadrangle Zeros_Quad; Zeros_Quad.a = 12.2;
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#5 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Aha..thanks...that was big help!
You mentioned "Scope" of a variable...I remember that from a lecture but I think it went straight over my head...I need to read up on the fundamentals of that... And that last bit...you made a new Object/reference called Zeros_Quad from Quadrangle...and assigned the variable a in Zeros_Quad with the value of 12.2...?
|
|
|
|
|
|
#6 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,312
My Mood:
Rep Power:
505
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Cool man, soz went to sleep LOL...
Scope means where you can 'see' the variable, where it is available to you.... Yeh you got the last bit right, but note that I directly accessed the 'a' variable and not through the setLengths Accessor..... If you did Quadrangle AP_Quad(12.3, 12.4, 12.5, 12.6); it would call the Constructor.... Without it calls the basic Accessor.... Actually I dunno if you can use it or not, coz you got at least one Constructor..... Not sure what would happen if you did create it as just: Quadrangle AP_Quad; Might fail....
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#7 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Hmm...I see..I see...
Cheers man...I'm just going to have to keep practising with my Editra..! Theres some niggly bits left here and there, but reading it is quite abstract...I just have to write and then compile the program to actually understand it fully...
|
|
|
|
|
|
#8 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Books I have now:
Java: How to Program 7th Edition. (By Paul & Harvey Deitel) This is my main reference book. Its just over 1500 pages and its a drag to carry to uni programming labs and I tried learning from this at the start of the reading week, but it failed because each chapter tells you everything about that topic...you wont find a mention of it anywhere else in the book after that. I'd probably call it the Java Bible... A Sams Book in PDF format. This gave me a pretty good headstart to OOP, but I found that it was too brief and I didn't grasp a lot of the fundamentals. An hour back, my package came from Amazon... Headfirst Java by O'reilly. This one seems pretty good so far! But I'm a bit pissed off it doesn't include Recursion. ![]() Regarding all the queries I had before...most of them are now answered thanks to this latest addtion. All I can say is thank god Java allocates memory automatically...
|
|
|
|
|
|
#9 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,312
My Mood:
Rep Power:
505
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
LOLLLLLLLL yeah, and I love the "new" keyword... You don't even wanna know how it was done in C.....
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#10 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I heard it was some LOOOOONG procedure to do it...lol.
To be honest...I think I prefer procedural programming....I dunno why...maybe cos its more orthodox to me... Fair enough OOP's got its benefits...but I dunno..its a bit niggly lol.
|
|
|
|
|
|
#11 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Waddup EG, this post looks long, but the problem I have is quite small;
I was doing an exercise where you had to write a program with two static methods to convert a decimal into binary and a binary into a decimal. I did the first method (decimal into binary), using recursion as it worked out pretty nicely, and heres the full program.... Code:
public class Exercise4decToBin{
Then at the command line I typed "java Exercise4decToBin 130" (where 130 was the argument passed). This gave me 10000010 (which is this right answer). Now I need to do the opposite... The method has to be of the form: Code:
public static int binToDec (String b){
//method code goes here....
}
It looks like this: Code:
String b = args[1];
System.out.println("The decimal representation of " + b " is " + binToDec(b));
I was wondering when you enter a binary number, say: 10000010 (decimal 130) into the command line when you're running a java program in DOS-Prompt, what does java take the number format as?...because the method signature I have to follow shows that a "String" of binary is converted (or cast as I think the right term is) into a "int" variable that is returned in decimal form. How do you think I should go about writing this method? Something to do with creating an array for each character entered? And then walking along the array and where each value = 1, store that in another variable and multiply it to the relevant power of that array using binary logic? Then output the final answer? Or is that too long winded lol...
|
|
|
|
|
|
#13 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,547
Rep Power:
704
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Well, I used to use Eclipse, but I didn't like the layout of it...
For small programs, I use Notepad++ and run it with Dos-Prompt. For other large pieces, I use Jcreator Pro...its pretty good!
|
|
|
|
|
|
#15 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,312
My Mood:
Rep Power:
505
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Errm, I'mma have to have a closer look at that method. But Java will take the command line argument as a String.
You'll be calling main() from your cmd, and main() looks like: public static void main (String [] args) Key thing is the parameter "args" which is a array of strings, it's unbounded so depending on number of words or strings you pass in, it will determine it's bounds. If you pass 130, you'll pass 130 as a string. If you pass 01010010 you'll pass as a string, for you to treat it as a int when you process it, you'll have to extract the string character (number) by number and convert it to an int. Probably with the use of a library function; or by looking at the ASCII numbers and comparing them that way. You don't want to convert to an int as a whole as it will probably chop off the zeros until the first non-zero number. So you'll have to read it as a string one character at a time. What happens when you pass in a value greater than 255?
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
![]() |
| Bookmarks |
| Tags |
| java, programming |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The Official GTA: IV Thread | DJ B4VVY | Technology, Hardware, Software and Games | 94 | 07-11-2008 17:34 |
| Euro 08 - Official Thread | Cheeky_Princess | Local, National and International Sports | 84 | 28-06-2008 11:07 |
| Official Music Thread | mindstate | Music and Radio | 26 | 16-05-2008 09:17 |
| official joke on the member above u thread | mindstate | Arcade Chat + Fun & Games Talk | 23 | 10-11-2007 22:45 |