×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Trying to teach myself Java

Trying to teach myself Java

Trying to teach myself Java

(OP)
Hey all,
I have some experience with programming with Perl, PHP and a tiny bit of C++. I am trying to teach myself Java. Of course the book I am using doesn't have solutions to the exercises with explanations at all. On one exercise it says to take bits of code from the examples and make them into a program that compiles and runs. I believe I have it right, but as I am a beginner I probably am missing something. I have a file called Exercise4.java and the code is as follows:

CODE

//: Exercise4.java

public class Exercise4
{
 class DataOnly
  {
   int i;
   double d;
   boolean b;
  }
 DataOnly data = new DataOnly();

 data.i = 47;
 data.d = 1.1;
 data.b = false;
}

However when I compile it, I get these errors:

CODE

Exercise4.java:13: <identifier> expected
 data.i = 47;
        ^
Exercise4.java:14: <identifier> expected
 data.d = 1.1;
        ^
Exercise4.java:15: <identifier> expected
 data.b = false;
        ^
3 errors

What am I doing wrong?

RE: Trying to teach myself Java

Java variables are not accesed like struct members. I think you should read the basis first, getting concept like classes and methods.

You can find a lot ot tutorials like this

Cheers,
Dian

RE: Trying to teach myself Java

To run your example you need a main() method.
I tried to do it at 2 ways:

1. With package-private class for the data structure

CODE

//: Exercise41.java

// package-private class
class DataOnly {
  int i;
  double d;
  boolean b;
}

// public class with main() method
public class Exercise41 {
  public static void main(String[] argv) {
    DataOnly data1  = new DataOnly();
    DataOnly data2  = new DataOnly();

    // set values
    data1.i = 47;
    data1.d = 1.1;
    data1.b = false;

    data2.i = 147;
    data2.d = 11.1;
    data2.b = true;    

    // prrint out
    System.out.println("data1.i ='" + data1.i +"'");
    System.out.println("data1.d ='" + data1.d +"'");
    System.out.println("data1.b ='" + data1.b +"'\n");

    System.out.println("data2.i ='" + data2.i +"'");
    System.out.println("data2.d ='" + data2.d +"'");
    System.out.println("data2.b ='" + data2.b +"'");
  }
}

CODE

C:\_mikrom\Work>javac -classpath . Exercise41.java

C:\_mikrom\Work>java -cp . Exercise41
data1.i ='47'
data1.d ='1.1'
data1.b ='false'

data2.i ='147'
data2.d ='11.1'
data2.b ='true'

2. With subclass for the data structure

CODE

//: Exercise42.java

// public class with main() method
public class Exercise42 {

  // static subclass
  static class DataOnly {
    int i;
    double d;
    boolean b;
  }

  public static void main(String[] argv) {
    DataOnly data1  = new DataOnly();
    DataOnly data2  = new DataOnly();

    // set values
    data1.i = 47;
    data1.d = 1.1;
    data1.b = false;

    data2.i = 147;
    data2.d = 11.1;
    data2.b = true;    

    // prrint out
    System.out.println("data1.i ='" + data1.i +"'");
    System.out.println("data1.d ='" + data1.d +"'");
    System.out.println("data1.b ='" + data1.b +"'\n");

    System.out.println("data2.i ='" + data2.i +"'");
    System.out.println("data2.d ='" + data2.d +"'");
    System.out.println("data2.b ='" + data2.b +"'");      
  }
}

CODE

C:\_mikrom\Work>javac -classpath . Exercise42.java

C:\_mikrom\Work>java -cp . Exercise42
data1.i ='47'
data1.d ='1.1'
data1.b ='false'

data2.i ='147'
data2.d ='11.1'
data2.b ='true'
Both examples above seems to work, but because I'm not experienced Java programmer, there is surely better solution.
smile
 

RE: Trying to teach myself Java

(OP)
Thanks for the tips! I added a main() method and compile worked. I always struggle learning something new at first especially when I haven't used classes in about 6 years.

@Diancecht I am trying to learn the basics and understand classes and methods and such. That is why I asked for help when I couldn't figure out what the problem was.

The book I am using is probably not the best, I didn't see anything about needing a method to make it run. I plan to re-read this First chapter again to see if I missed anything.

RE: Trying to teach myself Java

Hi

Personally I would think about adding a constructor to DataOnly, so its properties can be set on instantiation :

CODE --> Java

public class Exercise4 {
  class DataOnly {
    int i;
    double d;
    boolean b;
    DataOnly(int i, double d, boolean b)
    {
      this.i = i;
      this.d = d;
      this.b = b;
    }
  }
  DataOnly data = new DataOnly(47, 1.1, false);
}
( Note that I used BlueJ to test the class on its object bench without adding main() method or anything else. )

Feherke.
http://free.rootshell.be/~feherke/

RE: Trying to teach myself Java

I still think it is not a good practice to access class variables that way.

Cheers,
Dian

RE: Trying to teach myself Java

I agree with feherke and Diancecht: Use constructors and rather access attributes through getters/setters.  

RE: Trying to teach myself Java

(OP)
Thanks for your help all. I got it figured out. This is going to be a slow learning process. I am starting to remember the basics of C++ that I learned many years ago and it will help make Java easier.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close