Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to teach myself Java

Status
Not open for further replies.
Feb 4, 2011
53
CA
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?
 
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
 
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:
[COLOR=#0000ff]//: Exercise41.java[/color]

[COLOR=#0000ff]// package-private class[/color]
[COLOR=#2e8b57][b]class[/b][/color] DataOnly {
  [COLOR=#2e8b57][b]int[/b][/color] i;
  [COLOR=#2e8b57][b]double[/b][/color] d;
  [COLOR=#2e8b57][b]boolean[/b][/color] b;
}

[COLOR=#0000ff]// public class with main() method[/color]
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Exercise41 {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] argv) {
    DataOnly data1  = [COLOR=#804040][b]new[/b][/color] DataOnly();
    DataOnly data2  = [COLOR=#804040][b]new[/b][/color] DataOnly();

    [COLOR=#0000ff]// set values[/color]
    data1.i = [COLOR=#ff00ff]47[/color];
    data1.d = [COLOR=#ff00ff]1.1[/color];
    data1.b = [COLOR=#ff00ff]false[/color];

    data2.i = [COLOR=#ff00ff]147[/color];
    data2.d = [COLOR=#ff00ff]11.1[/color];
    data2.b = [COLOR=#ff00ff]true[/color];    

    [COLOR=#0000ff]// prrint out[/color]
    System.out.println([COLOR=#ff00ff]"data1.i ='"[/color] + data1.i +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data1.d ='"[/color] + data1.d +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data1.b ='"[/color] + data1.b +[COLOR=#ff00ff]"'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]); 

    System.out.println([COLOR=#ff00ff]"data2.i ='"[/color] + data2.i +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data2.d ='"[/color] + data2.d +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data2.b ='"[/color] + data2.b +[COLOR=#ff00ff]"'"[/color]); 
  }
}
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:
[COLOR=#0000ff]//: Exercise42.java[/color]

[COLOR=#0000ff]// public class with main() method[/color]
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Exercise42 {

  [COLOR=#0000ff]// static subclass[/color]
  [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] DataOnly {
    [COLOR=#2e8b57][b]int[/b][/color] i;
    [COLOR=#2e8b57][b]double[/b][/color] d;
    [COLOR=#2e8b57][b]boolean[/b][/color] b;
  }

  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] argv) {
    DataOnly data1  = [COLOR=#804040][b]new[/b][/color] DataOnly();
    DataOnly data2  = [COLOR=#804040][b]new[/b][/color] DataOnly();

    [COLOR=#0000ff]// set values[/color]
    data1.i = [COLOR=#ff00ff]47[/color];
    data1.d = [COLOR=#ff00ff]1.1[/color];
    data1.b = [COLOR=#ff00ff]false[/color];

    data2.i = [COLOR=#ff00ff]147[/color];
    data2.d = [COLOR=#ff00ff]11.1[/color];
    data2.b = [COLOR=#ff00ff]true[/color];    

    [COLOR=#0000ff]// prrint out[/color]
    System.out.println([COLOR=#ff00ff]"data1.i ='"[/color] + data1.i +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data1.d ='"[/color] + data1.d +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data1.b ='"[/color] + data1.b +[COLOR=#ff00ff]"'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]); 

    System.out.println([COLOR=#ff00ff]"data2.i ='"[/color] + data2.i +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data2.d ='"[/color] + data2.d +[COLOR=#ff00ff]"'"[/color]);
    System.out.println([COLOR=#ff00ff]"data2.b ='"[/color] + data2.b +[COLOR=#ff00ff]"'"[/color]);      
  }
}
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.
:)
 
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.
 
Hi

Personally I would think about adding a constructor to DataOnly, so its properties can be set on instantiation :
Java:
[b]public[/b] [b]class[/b] [COLOR=mediumorchid]Exercise4[/color] [teal]{[/teal]
  [b]class[/b] [COLOR=mediumorchid]DataOnly[/color] [teal]{[/teal]
    [maroon]int[/maroon] i[teal];[/teal]
    [maroon]double[/maroon] d[teal];[/teal]
    [maroon]boolean[/maroon] b[teal];[/teal]
    [COLOR=darkgoldenrod]DataOnly[/color][teal]([/teal][maroon]int[/maroon] i[teal],[/teal] [maroon]double[/maroon] d[teal],[/teal] [maroon]boolean[/maroon] b[teal])[/teal]
    [teal]{[/teal]
      [b]this[/b][teal].[/teal]i [teal]=[/teal] i[teal];[/teal]
      [b]this[/b][teal].[/teal]d [teal]=[/teal] d[teal];[/teal]
      [b]this[/b][teal].[/teal]b [teal]=[/teal] b[teal];[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
  [COLOR=orchid]DataOnly[/color] data [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]DataOnly[/color][teal]([/teal][purple]47[/purple][teal],[/teal] [purple]1.1[/purple][teal],[/teal] [b]false[/b][teal]);[/teal]
[teal]}[/teal]
( Note that I used BlueJ to test the class on its object bench without adding [tt]main()[/tt] method or anything else. )

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

Cheers,
Dian
 
I agree with feherke and Diancecht: Use constructors and rather access attributes through getters/setters.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top