The Asian Place - Desi Forum - Asian Forum  

Go Back   The Asian Place - Desi Forum - Asian Forum > Education - University - College - School > University
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 ..."
Reply
 
LinkBack Thread Tools
Old 04-11-2008, 22:25   #1 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Question 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 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...?

Signature

Last edited by Zero; 04-11-2008 at 22:28.
Zero is offline
 
Reply With Quote
Old 04-11-2008, 22:27   #2 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

The formatting messed up a bit...so ignore the coloured fonts...

Signature
Zero is offline
 
Reply With Quote
Old 04-11-2008, 23:00   #3 (permalink)
Evil_Genious
Loyal Member
 
Evil_Genious's Avatar
 
Location: The Future
Gender: Male Male
Country:
Posts: 3,312
My Mood:
Rep Power: 505
Rep Level: Evil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond repute
Default

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;

Signature

"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"
Evil_Genious is offline
 
Reply With Quote
Sponsored Links
Old 04-11-2008, 23:09   #4 (permalink)
mindstate
Loyal Member
 
mindstate's Avatar
 
Location: uk
Gender: Male Male
Country:
Posts: 3,659
Rep Power: 272
Rep Level: mindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond reputemindstate has a reputation beyond repute
Default

mindstate is offline
 
Reply With Quote
Old 04-11-2008, 23:13   #5 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

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...?

Signature
Zero is offline
 
Reply With Quote
Old 05-11-2008, 22:13   #6 (permalink)
Evil_Genious
Loyal Member
 
Evil_Genious's Avatar
 
Location: The Future
Gender: Male Male
Country:
Posts: 3,312
My Mood:
Rep Power: 505
Rep Level: Evil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond repute
Default

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....

Signature

"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"
Evil_Genious is offline
 
Reply With Quote
Sponsored Links
Old 05-11-2008, 22:53   #7 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

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...

Signature
Zero is offline
 
Reply With Quote
Old 06-11-2008, 14:56   #8 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

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...

Signature
Zero is offline
 
Reply With Quote
Old 06-11-2008, 21:42   #9 (permalink)
Evil_Genious
Loyal Member
 
Evil_Genious's Avatar
 
Location: The Future
Gender: Male Male
Country:
Posts: 3,312
My Mood:
Rep Power: 505
Rep Level: Evil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond repute
Default

Originally Posted by Zero View Post
thank god Java allocates memory automatically...

LOLLLLLLLL yeah, and I love the "new" keyword... You don't even wanna know how it was done in C.....

Signature

"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"
Evil_Genious is offline
 
Reply With Quote
Sponsored Links
Old 06-11-2008, 22:15   #10 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

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.

Signature
Zero is offline
 
Reply With Quote
Old 12-11-2008, 19:52   #11 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Question Number Conversion Help

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{
public static String decToBin (int n){
if (n==0) return ""; if (n%2==0) return decToBin(n/2) + "0"; else return decToBin(n/2) + "1"; }
public static void main (String [] args){
int d = Integer.parseInt(args[0]); System.out.println("The binary representation of " + d " is " + decToBin(d));
}
}
You run "javac *.java" and this compiles the file...

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....
}
I've written the second portion of the "main" method as I have to incorporate both static methods into one class file.

It looks like this:

Code:
 
String b = args[1];
System.out.println("The decimal representation of " + b " is " + binToDec(b));
The reason why "String b = args[1]" is due to "args[0]" being assigned to another method (decToBin)? Thats the right way to put it I presume.

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...

Signature
Zero is offline
 
Reply With Quote
Old 13-11-2008, 20:05   #12 (permalink)
vertigo
Forum Addict!
 
Location: in space
Age: 24
Gender: Male Male
Country:
Posts: 1,681
Rep Power: 115
Rep Level: vertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant future
Default

man im stuck on my programming....i fookin hate java

btw zero what text editor program are u using to type ur java code

Signature




vertigo is offline
 
Reply With Quote
Sponsored Links
Old 13-11-2008, 21:25   #13 (permalink)
Zero
Respected Member
 
Zero's Avatar
Gentleman's Club
 
1 Highscore
Location: .
Gender: Male Male
Posts: 14,547
Rep Power: 704
Rep Level: Zero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond reputeZero has a reputation beyond repute
Default

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!

Signature
Zero is offline
 
Reply With Quote
Old 13-11-2008, 21:30   #14 (permalink)
vertigo
Forum Addict!
 
Location: in space
Age: 24
Gender: Male Male
Country:
Posts: 1,681
Rep Power: 115
Rep Level: vertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant futurevertigo has a brilliant future
Default

hmm eclipse yea i dnt like it aswell not used to it

i use textpad which is basic, does the job

Signature




vertigo is offline
 
Reply With Quote
Old 13-11-2008, 21:43   #15 (permalink)
Evil_Genious
Loyal Member
 
Evil_Genious's Avatar
 
Location: The Future
Gender: Male Male
Country:
Posts: 3,312
My Mood:
Rep Power: 505
Rep Level: Evil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond reputeEvil_Genious has a reputation beyond repute
Default

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?

Signature

"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"
Evil_Genious is offline
 
Reply With Quote
Sponsored Links
Reply

Bookmarks

Tags
java, programming


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

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT +1. The time now is 15:56.

Contact Us - The Asian Place - Sitemap - Privacy Statement - Top

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
© The Asian Place 2004-2008 All Rights Reserved