Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Basic syntax/structure of Java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
poseidon




PostPosted: Sun Jul 13, 2003 12:13 pm   Post subject: Basic syntax/structure of Java

I have a few questions relating to the basic structure/syntax of Java (ver 2)

1) What does "static" mean? eg. public static void main (String text [ ] ) {

1.1) Does the first method ALWAYS have to be "main"?

2) from prev example, (String text [ ] ) ). Is "text" an identifier or an actual keyword in this case? Also, ive seen the [] placed in many different positions (eg after String []) or (String args []). What do these mean?

3)Im using Dr Java on Win XP. How do I run the program(not compile). Ive tried doing it through command prompt but i can never get it to actually run.

4) Does any1 kno where I can get a good beginner java tutorial designed for some1 familiar w/ onyl Turing?

5)What are constructors and when must they be used?


Thanks for any help provided.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Jul 13, 2003 12:51 pm   Post subject: (No subject)

1] static.. (have u looked at the "Hello World Tutorial"?)

2] the method the compiler will call when u run ur java app, will be the main.

3] public static void main(String[] args).. u see text in ur example was the variable.. in this case args. It is a variable declared as an array of type String.. if u were to declare a varaible of type string u do something like this:

code:

String str;
str = "Hello World";


to declare an array of type String u do this:

code:

String[] strs;

strs = new String[5];

str[0] = "Hello";
str[1] = "World";
...


as u see the [] just decalres an array.

again read the Hello World tutorial
rizzix




PostPosted: Sun Jul 13, 2003 1:03 pm   Post subject: (No subject)

some folks declare arrays the old C way:

code:

String args[];


the Java (prefered) way is like this:

code:

String[] args;
PaddyLong




PostPosted: Sun Jul 13, 2003 2:31 pm   Post subject: (No subject)

constructors are pretty important and it is unfortunate that turing doesn't have them ...

but basically when you call your new someObject statement to create an object, you are really calling the constructor and so when you do the new someObject you need to have the parameters for the constructor. What constructors do is set initial values for the object's data.
poseidon




PostPosted: Sun Jul 13, 2003 4:03 pm   Post subject: thxs

Hmmm .... thx, i get most of it.

Quote:


some folks declare arrays the old C way:

Code:

String args[];



the Java (prefered) way is like this:

Code:

String[] args;



so ..... theres no real difference as far as Java is concerned rite?
rizzix




PostPosted: Sun Jul 13, 2003 5:14 pm   Post subject: (No subject)

no difference

but do use the prefered way (it's what most programmers expect)
poseidon




PostPosted: Sun Jul 13, 2003 7:50 pm   Post subject: (No subject)

Alrite, I guess ill use tat proper way then.

What are the usages of "extends JFrame" and "throws IOException"?

Haha, sry for all the qs. Im jsut falling behind this workshop im taking and so ive been programmign a lot these few days.

As a result, ive been encountering many problems and little things I cant seem to fidn explanations for.
poseidon




PostPosted: Sun Jul 13, 2003 8:03 pm   Post subject: Prog im wrkin on (bugged .. <b> seriously </b> b

Sry for creating another post (instead of adding this w/ my prev psot) but this is really long and I was afraid there was a limit.

I have this prob I havta do and I've been stuck on it for a really logn while now. CAn u guys correct any errors (in syntax or watever) and help me w/ the last part?


--------------------------------------

Here the question:


Write a subclass of JFrame called TilingWindow that has the following methods.

- Write an int method called widthRatio that has one parameter, a JFrame j and returns the width of this window divided by j's width.

-Write a boolean method called can TileSideways that has two parameters, an int i and a JFrame j, and returns true if i copies of j will fit side by side inside this window. For example, if i is 8 and j is 50 pixels wide and this window is 430 pixels wide, then the result is true because 8 copies of j can fit side by side. However, if i is 10, then the result is false because 10 copies of j wont fit: 10 x 50 > 430.

-Switch roles: s2 drives and s1 navigates. Write a boolean method called canTile that has two parameters, and int i and a JFrame j and returns true if i copies of j will fit inside this window in a grid pattern. Hint: figure out how many times j fits horizontally and how many times it fits vertically.

----------------------------------------------------

-
So far, this is wat I have. but its really bugged. Cause ive jsut really started java programming like 3 days ago and im an abs beginner.

Thanks for any help tho, I really appreciate it (esp rizzix and paddylong, u guys have been really helpful)

code:



//imports the neccessary packages needed to fidn the screen dimension
import java.awt.*;

public class TilingWindow {
 
  // stores the screen dimensions in d
  Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
  int screenWidth = (int) d.getWidth(); 
  int screenHeight=(int)d.getHeight();
 
  //int method
  /*probably wrong, I've only created String methods b4
   * so my int and boolearn methods are probably wrong
   */
  public int widthRatio (JFrame j) {
   
    return j.getWidth/screenWidth;
   
  }
 
  public boolean canTileSideways (int i; JFrame j) {
   
    if (i*j<=screenWidth) {
     
      //can i actually do this?  cause i understand boolean isnt a variable
      boolean= true
    }
   
    else {
     
      boolean = false
       
    }
   
    //again, probably wrong.  But since i have no experience in returning things
    //this is basically a stab int eh dark
    return boolean
   
  }
 
  public boolean canTile (int i; JFrame j) {
   
    /*I have a few theories re: how to do this one.  But i cant really put
     * them into actual code
     */
   
  }
   
   
 
}



rizzix




PostPosted: Sun Jul 13, 2003 9:17 pm   Post subject: (No subject)

hey there it seams ur a VB programmer!
i'll help,, but i won't solve it for u. i'll tell where u go worng. what u are all confused about..

ok first things first:
code:

public boolean isTrue() {
    return true;
}


u see.. the method header, public boolean isTrue() explains the basic things the compiler (and the programmer) needs to know about your method (or function.. whatever).

in this case it reads like this:
i'm declaring a public method (one that can be called by anyone) and
it returns a value of type boolean (ture or false) [mind u boolean is a keyword in java] , and the method is called isTure.. which takes no arguments.

now the body of the method must coincide with the header, i.e it has to return a value of type boolean.

to do this we just write return true; if u happen to add a couple of extra lines after the return statement, those lines will be ignored. u can control this with logical constructs (if block), just like u tried in ur code.

true and false are the only to values a boolean variable can hold.

so if u need to decalre a variable of type boolean u would do this:
code:

boolean condition;

now in this case it is simply declared.
u can assign it a value like this:
code:

condition = true;

if the method u wrote needs to return this value u do this:
code:

return condition;



now some methods/functions need arguments. You can declare the method to accept arguments like this:
code:

public void setSomething(int i, int k) {
     ...does_something;
}


as u see we seperate multiple arguments with a comma


one last thing: never forget to terminate ur statments with the semicolon. its like a full-stop in english and many other languages.
rizzix




PostPosted: Sun Jul 13, 2003 9:30 pm   Post subject: (No subject)

k.. the extends keyword in java just tell the compiler that the class u are writing is the subclass of ...

for example:
code:

class MyFrame extends JFrame {
     int i;
     int k;     

     MyFrame(String title, int i, int k) {
          super (title);
          this.i = i;
          this.k = k;
     }

     public int getI() {
          return i;
     }
 
     public int getK() {
          return k;
     }
}


now this class u created is a JFrame.
it can be used just like a JFrame but you can also use the new abilities you added, in this case to get the values of i and k.

there are some interesting things i've written up there.
first the new keyword super

super is well.. a special keyword that represents the super-class of the current class, i.e the class one step higher up the hierarchy.

in this case we are calling the base class' constructor, (which is JFrame's constructor) that take one argument of type string.

in this case it sets the title of the JFrame.

i also use the keyword this which represents the current class's object instance.
as u can see in this case it was important. both the variables were of the same name, the MyFrame's members and the arguments.

by specifying this.member we are being very specific that we want this class's member variable to be set to...

one more important thing.. once u decalre a constructor.. NO constructors will be inherited. u'll have to manually overload all these constructors ur self.

u see..? feel free to ask any more questions
rizzix




PostPosted: Sun Jul 13, 2003 9:34 pm   Post subject: (No subject)

exceptions are explained in the Exception Handling tutorial
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: