
-----------------------------------
poseidon
Sun Jul 13, 2003 12:13 pm

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.

-----------------------------------
rizzix
Sun Jul 13, 2003 12:51 pm


-----------------------------------
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.. 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:


String str;
str = "Hello World";


to declare an array of type String u do this:


String[] strs;

strs = new String[5];

str[0] = "Hello";
str[1] = "World";
...


as u see the  just decalres an array.

again read the [url=http://www.compsci.ca/bbs/viewtopic.php?t=1082]Hello World tutorial

-----------------------------------
rizzix
Sun Jul 13, 2003 1:03 pm


-----------------------------------
some folks declare arrays the old C way:


String args[];


the Java (prefered) way is like this:


String[] args;


-----------------------------------
PaddyLong
Sun Jul 13, 2003 2:31 pm


-----------------------------------
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
Sun Jul 13, 2003 4:03 pm

thxs
-----------------------------------
Hmmm ....  thx, i get most of it.  



some folks declare arrays the old C way: 

Code: 

String args

so ..... theres no real difference as far as Java is concerned rite?

-----------------------------------
rizzix
Sun Jul 13, 2003 5:14 pm


-----------------------------------
no difference

but do use the prefered way (it's what most programmers expect)

-----------------------------------
poseidon
Sun Jul 13, 2003 7:50 pm


-----------------------------------
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
Sun Jul 13, 2003 8:03 pm

Prog im wrkin on (bugged .. &lt;b&gt; seriously &lt;/b&gt; 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)




//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