A question by a beginner

Hi All, i'm a beginner and i'm studing on a book the foundamental of Java. I have followed the book and i have write my first scripts that is:
* SimpleCar.java
* Created on February 4, 2007, 2:04 PM
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
* @author Emanuele Mignosa
public class SimpleCar {
private String manufacturer = "Sconosciuto";
private String color = "Bianco";
private double miles = 0;
private final double KMTOMILE = 0.62137;
private final double MILETOKM = 1.60934;
public String getManufacturer() {
return manufacturer;
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
public String getColor() {
return color;
public void setColor(String color) {
this.color = color;
public double getMiles() {
return miles;
public void setMiles(double miles) {
this.miles = miles;
public void drive (double kilometres){
this.miles += Math.round(
(kilometres * KMTOMILE)*100.)/100.;
public double getKilometres(long miles){
return Math.round((miles * MILETOKM)*100.)/100.;
public String carStatus() {
String status = "old";
if (this.getMiles()<= 10) {
status = "very new";
else if(this.getMiles()<= 1000){
status = "new";
else if(this.getMiles()<= 10000){
status = "almost new";
else if(this.getMiles()<= 20000){
status = "in perfect conditions";
else if(this.getMiles()<= 50000){
status = "used";
else if(this.getMiles()<= 150000){
status = "old";
return status;
and a next script that invoke the class SimpleCar that is :
[public class SimpleCarInvoker {
     /** Creates a new instance of SimpleCarInvoker */
    public static void main (String[] args){
SimpleCar car = new SimpleCar();
car.setManufacturer("Audi");
car.setColor("blu");
car.drive(10000);
System.out.println (String.format(
"You are driven a %s %s that has this condition : %s",
car.getManufacturer(),
car.getColor(),
car.carStatus()));
untill this point all perfect
the book advices this script :
[public class SimpleCar{
    private String manufacturer = "Sconosciuto";
    private String color = "Bianco";
    private double miles = 0;
    private final double KMTOMILE = 0.62137;
    private final double MILETOKM = 1.60934;
    /** Creates a new instance of SimpleCar1*/
    public SimpleCar() {
        this.setManufacturer("Porche");
        this.setColor("Blu");
public SimpleCar(String manufacturer) {
this();
this.setManufacturer(manufacturer);
public SimpleCar(double miles) {
this();
this.setMiles(miles);
public SimpleCar(String manufacturer, double miles) {
this.setManufacturer(manufacturer);
this.setMiles(miles);
public SimpleCar(String manufacturer, String color) {
this.setManufacturer(manufacturer);
this.setColor(color);
public SimpleCar(String manufacturer, String color, double miles) {
this.setManufacturer(manufacturer);
this.setColor(color);
this.setMiles(miles);
I have delete the old java class, and i have created a new java class with the same name, as i see on the book, but there were many problems and i don't know what i must change.
Thanks a lot in advance
Emanuele
P.S i think there is a error in the book, because the new scripts don't create the metod "set", or i must put insert the new script in the old script, but class is the same.

I'm not sure if this will do it. That's a lot of code to sort through.
Before the change, you didn't have any constructors. Now you're adding them in. So if you have Strings as instance variables, a constructor won't be able to change them.
Try just saying private String manufacturer; at the beginning.
instead of keeping the " ='Sconosciuto' " part. Strings are pointers and they cannot be reset.
I've never seen a book use setter methods to set the variables. If you haven't defined your variable manufacturer yet, you should be able to just say inside your constructor:
manufacturer = "Audi", e.g.
Hope it helps

Similar Messages

  • Basic Perl questions from Perl beginner now using Leopard

    Question: I used to dabble in Perl programming (beginner!!) and want to continue my dabbling with my new iMac running Leopard.
    Which Perl do I need to install?
    Where do I get it from?
    What tricks / concerns are there in the installation?
    What's the best source of information (books?, internet?) for learning about using/programming Perl on Leopard?
    Any other advice that you believe would be helpful would be appreciated.
    Many thanks!! D

    I used to use MacPerl as well, it's even better now that Perl is pre-installed and native.
    The two thing that took me the most time to figure out are:
    Line endings. All my old scripts had carriage return - line feed as end of line delimiters.
    On linux they have to be line feed '\x0A'. You do that by setting it in your text editor. I use BBEdit or Text Mate to write perl, although I'm pretty sure that using TextEdit will work, and will have line feed line endings.
    Setting the script's executable bit. A script file needs to have a bit set in the linux filesystem to tell linux it's executable. You do that by opening the terminal and typing chmod +x then drag your script file into the terminal and pressing return.
    That's detailed here:
    http://www.oreilly.com/pub/a/mac/2003/11/07/scripting_osx.html
    Also there are 2 main ways to run your script.
    One is by opening Terminal and +typing perl+ then dragging your script into the terminal window and hitting return. if you want to do it this way the very first line in your perl script needs to look like this:
    #!/usr/bin/perl
    The other way (which is only slightly easier) is by dragging your script into the terminal window and hitting enter, no typing perl every time. To do this the very first line of your perl script needs to be:
    #!/usr/bin/env perl
    Another huge tip is if you are in the Terminal window hitting command-up arrow will show you the last command you typed in, so you can hit enter and re-run it.
    Other than that stuff I can't remember any differences between regular linux perl and Leopard perl. I still refer to the Camel book I bought 15 or so years ago.
    So type into a text editor:
    #!/usr/bin/perl
    use strict;
    use warnings;
    print "Hello World\n";
    Then save the file as Hello.pl
    open terminal and type *chmod +x*
    drag Hello.pl into the terminal and press return
    Type perl into the Terminal
    drag Hello.pl into the terminal and press return.
    and it should print.

  • A few questions from a beginner

    Please don't hit me because of my questions...
    1. Is there a build in method (class or whatever) to increase an array size? I don't want to copy the array each time into a larger array and then setting the pointer of the new larger array to be the same as the old smaller array. I mean a method like Array.increase(param) or something like that.
    2. Is there a way to find out how many dimension an array has? A build in method like .length
    3. Can I convert a array into a vector? If yes, how?
    4. The Math.round() method accepts only one parameter which contains the double number to be rounded. But it does't have a second parameter by which I can tell the method to round a number to the second (third or whatever) number after the dot. Is there a method like that somewhere else or do I need to program it for myself?

    ::smack::
    there, now that's out of the way..
    1) No.. arrays are static in length, which is set upon instantiation. If you need a datatype to store multiple objects that will need to increase in size, you should use Vector.
    2) I believe I'm right in saying that there's not really a way to check that. A multidimensional array is basically an array of arrays. I guess you could know that if you got myarray[0] and did a check to find out what datatype it was, you'd know that if the index contained an array, it'd be 2D ... then you could check that array, etc. However, since you are declaring the array, and when passing the array, you'd have to specify what it was (ie public void foo(int[][][] my3dIntArray) ) you should never not know the dimensions.
    3) Yes. But you'll have to loop through your array and add items to the Vector.
    4) You're right, it doesn't. Now that you mention it though, I can't think of a method that does that.... but it shouldn't be too hard for you to work out your won.

  • Very simple question from a beginner

    I am currently doing a little exercise, but I ran into a problem. I have written a class that supports operations on rational number. The fields are two long variables, one each that stores the numerator and denominator.
    I have made add, subtract, multiply, and divide methods...
    1. what I need help in is: beingh able to store the rational number in reduced for, with the denominator always positive
    2. constructing a toString method, equals method, and compareTo method.
    3. it also says "make sure the toString method correctly handles the case in which the denominator is zero by throwing an exception.
    I have been working on this for a long time now, an I am stuck at this point.
    I know this will be very simple for you guys and gals, but I am a beginner. Please offer some input, advice, and/or some code to help me get past this problem.
    Thanks so much,
    Jason

    ok, here is what I have so far
    public class Rational {
         public long num, den;
         public Rational(long n,long d) {
              this.num = n;
              this.den = d;
         public static void main(String args[]) {
              Rational r = new Rational(1,2);
              Rational s = new Rational(3,4);
              Rational rmuls = r.multiply(s);
              Rational rdivs = r.divide(s);
              Rational radds = r.add(s);
              Rational rsubs = r.subtract(s);
              r.print();
              s.print();
              rmuls.print();
              rdivs.print();
              radds.print();
              rsubs.print();
         public void print(){
              System.out.println(this.num+ "/" +this.den);
         public Rational multiply(Rational t){
              long n;
              long d;
              n = this.num*t.num;
              d = this.den*t.den;
              Rational answer = new Rational(n,d);
              return answer;
         public Rational divide(Rational t){
              long n;
              long d;
              n = this.num*t.den;
              d = this.den*t.num;
              Rational answer = new Rational(n,d);
              return answer;
         public Rational add(Rational t){
              long n;
              long d;
              n = (t.den*this.num)+(t.num*this.den);
              d = t.den*this.den;
              Rational answer = new Rational(n,d);
              return answer;
         public Rational subtract(Rational t){
              long n;
              long d;
              n = (t.den*this.num)-(t.num*this.den);
              d = t.den*this.den;          
              Rational answer = new Rational(n,d);
              return answer;
         //public int compareTo (Rational t)??????

  • Question from a beginner

    Hi, all, I'm new to JSP and JavaBeans. Now I have to develop a web site with these technology.
    My first question is:
    Can I just put all database access(query, update, delete...) into JavaBean and create a bean for every table I have. Is this a good idea or I'd better follow the MVC model? Am I able to call a method in such a bean to access database?
    I've tried it, but I got a "NoSuchMethodError".
    Thank you so much.

    Using beans is a pretty good way to display database content in your jsp - it's certainly better than having all your code in JSP scriptlets (ie. between <% ... %> tags). The idea is to have as little actual java code in your jsps (separate presentation from business logic).
    The ultimate goal is an MVC architecture. Struts (http://jakarta.apache.org) is a framework which helps you to set this up and takes a lot of the pain out of setting up your own.
    HOWEVER ... I'd say that if you're just starting out with java and jsp then an MVC framework might confuse the hell out of you and your colleagues. If your app is not too complex then you might prefer to start off simply using beans in your pages until you get up to speed with all the other jsp issues. It's virtually impossible to get your jsp webapp architecture perfect the first time round, so perhaps set your sights on a simple model.

  • Question from a beginner, really need help

    I am starting to do a little programming these days. The question is about the (program file) format to provide to the end users.
    I can use "javac" "java" to compile or run my code in a command prompt window, or use some development tool. But I do not expect the users of my program to install the jdk (or other software) in order to run the program. How to deal with this?
    What is the usual last format of a Java Program?
    Hope I have made my question clear.
    Thanks
    Xiaohua

    It depends, a good solution is to pack it into a jar file and make it executable, but if you are new to Java (I have no idea how experianced you are) it might give you more trouble than it's worth in the beginning.
    You could distribute your .class file (the .java files are only source and can not be executed) and a .bat file which would contain the command to launch the applicaiton. That's how I would do it as a newbie.
    Check out an installer program like Inno Setup ( http://www.jrsoftware.org/isinfo.php ) if you want a neat way to distribute your files. You can specify a .bat file as being the one executed when the applicaiton is started, that way your program could be run from a start menu entry (sorry, I'm assuming that you are using Windows here, to maintain cross-platform abilities this won't work)
    Does this give you an idea? There are many ways to distribute an application. You should also read up on Java Webstart...

  • Few questions from a beginner

    hi there , i have a few questions first thing
    is it possible to make every connector that comes out of a specific shape the same color , im building a map where 1 shape gives of multiple connections, i want them all to be in the same color, and the connections coming from the second shape are all in
    a different color from the first shape
    second question: how can i select all the connectors that pass in a particular area
    i selected in the option select shapes partially in area, but it selects connectors that are NOT in the area that i selected
    thank you very much for your help

    There is no out of the box way for the first question but you can write a macro to do that. 
    Shape properties Shape.Connects and Shape.FromConnects can be used for selecting all
    outgoing connectors of a shape programmatically. And then appropriate formatting (colors) can be applied on selection by selection.

  • A question from a beginner

    I'm teaching a introductory programming course in java to some folks, and explaining variable lifetimes and the need for object member variables to store values longer than just a method call.
    So the example given was of a parameter being saved in a member variable
    The question arose, "Why can't you just specify the member variable as the parameter?"
    To which i gave the obvious reasons.
    When the class was over, i thought about it some more, and except for syntactic unpleasantries, there is no reason why this couldn't be done. Given that this is a common idiom, its odd that you can't do that.
    It's only not odd, because it's never been done before.
    Just thought it was an interesting thought that new-person thought produced.

    To paraphrase the JLS: When the method or constructor is invoked, the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor. The identifier may be used as a simple name in the body of the method or constructor to refer to the formal parameter. Taking your example:
    public class Foo {
      private int input;
      public void setInput( int Foo.input ) {}
    }Here you are not initializing any newly created parameter variables, and you have no identifier that may be used as a simple name in the body of the method or constructor to refer to the formal parameter. This breaks another part of the language specification which states that formal parameters are referred to only using simple names, never by using qualified names. You might also get into issues with the float-extended-exponent value set and the double-extended-exponent value set.
    Alternatively, you may just be suggesting that the compiler perform qualified name erasure to transform:
    public class Foo {
      private int input;
      public void setInput( int Foo.input ) {}
    }to:
    public class Foo {
      private int input;
      public void setInput( int input ) {
        this.input = input;
    }If this is the case, isn't syntactic sugar supposed to makes things "sweeter" to use? IMHO, this syntax isn't.

  • Connection questions..(beginner)

    In my case, i use jdbc and need to connect 2 remote MS SQL server named SQLNT1 and SQLNT2\SQLNT2 (don't ask me why this name so strange)... both has a database named "bees".
    Theres 's some problems in the connections.. I 've tested the followings:
    when String url = "jdbc:microsoft:sqlserver://SQLNT1:1433"; (work)
    when String url = "jdbc:microsoft:sqlserver://SQLNT1:1433:bees"; (fail)
    when String url = "jdbc:microsoft:sqlserver://SQLNT2\\SQLNT2:1433"; (fail)
    = "jdbc:microsoft:sqlserver://SQLNT\\SQLNT2:1433:bees"; (fail)
    I want to connect to the databases of each server, however, I can just connect to SQLNT1, but still cannot get the database "bees".... >_<... ..Also, cannot make any connection to SQLNT2\SQLNT2...
    please help...
    thank in advance
    LEO

    when String url =
    "jdbc:microsoft:sqlserver://SQLNT2\\SQLNT2:1433";
    (fail)yes...it can connect to the Db. by both above methods... thkx..
    Change the name of the server (sql) to some meaningful and realistic name.i am sorry that, i cannot change the SQL server name for SQLNT2\SQLNT2
    _<LEO

  • Oracle Triggers  - A few questions from a beginner to oracle.

    Hi,
    1. If we cannot COMMIT within a trigger, then how is it possible to use TRIGGERS to INSERT rows into an audit table that will track any INSERT/UPDATE attempt on a table. I have to record even failed attempts.
    2. Is it possible to make API calls from within a TRIGGER? (This API will write out data from the table being INSERTED/UPDATED into corresponding tables in a MYSQL database (somewhat like replication))
    3. If 2 is not possible, we are planning to write updates to an intermediate table that will be checked by a webservice every few minutes and in turn update the MYSQL db.
    Please help/advise.
    Thx.
    Vis.

    1. use autonomous transactions.
    2. if the API a PL/SQL code, yes you can call PL/SQL from
    the trigger body.
    To connect Oracle and MySQL, following thread might help:
    Re: Oracle to MySQL database connectivity
    Message was edited by:
    Pierre Forstmann

  • Compression Question from a beginner

    I'm trying to export my first video. I'm a little lost when it comes to all the compression options. Basically I have a 2min video with sound that I want to upload to youtube, but its fairly important that the sound quality isn't diminished too badly. I'm guessing that I want to export to an AVI or MPEG4, but again, I'm new to this. Can anyone make any recommendations? Much appreciated.

    This is for Compressor, but the principles are the same for exporting from FCE.
    http://www.kenstone.net/fcphomepage/youtube_compressorgary.html

  • A simple question from a beginner

    how do i check for a null reference? eg:
    String val = null;
    // do some stuff with a hashtable...
    // now want to check if val is still null
    if (val.equals(null)) // this is where i get a NullPointer Exception
    System.out.print ("val is still null!");
    thanx in advance

    if (val == null) ...

  • Questions from a beginner

    does Jdeveloper have a servelet containers?
    i have to choose bettween eclipse and Jdevelopper which one shall i choose and why?
    i have to call "api" written with "c" to a java application ,are there a solution?
    thank u

    As already stated, Jdeveloper comes with an embedded version of the OC4J J2EE container, which supports JSP's, Servlets, EJBs, etc...
    Our organisation is currently migrating away from a combination of WSAD 5.1, which is an Eclipse based IDE, and MyEclipse, and standardizing on JDeveloper 10.1.3 - the feedback in almost all areas has been extremely positive.
    The only area where Eclipse seems superior is in its interfaces to CVS. JDeveloper works OK in this area but can be clunky with accessing repositories on WAN connections.
    Also, if you plan on using Maven2 - its not really documented anywhere, but a workable plugin was available as part of the original ADF Faces release to Apache. I can't find it right now, but it would be great if Oracle would officially release it or contribute it to the Apache Maven group <hint/>

  • A beginner question from a beginner in JDBC

    Hi,
    I am trying to execute a query on the database and I have a really strange problem:
    When i execute the query, the system answers me "invalid column name?". I try to male query to find the name of all the column and the answer is good (all the column exist).
    The qu

    Did you check the spellings of column names?
    -XL

  • Basic question from a beginner

    I am looking for a solution to this problem: I wish to record some classical piano for an audition DVD. I have a video camera, but the dynamic range and fidelity of the microphone are not very good. I'd like to simply strip out the audio and replace it with input from a good microphone. (I have a good microphone and USB input for it.) The audio/video syncing has to be seamless. In the past I've recorded audio using GarageBand, and I've used iMovie from camera input (A&V) for separate projects. I hear the problem is that GarageBand samples at 44 kHz (and cannot be adjusted) while video is normally recorded a 48 kHz. What do I need in terms of software to do this job? Would you recommend FC Express, strip out the audio, and find some audio recording software that can sample at 48 kHz? Any recommendations? An economical solution would be nice. I appreciate the help in getting me started.
    Message was edited by: joelmg

    Does your camera allow use of an external mic? That would be the simplest solution.
    If you need to record the audio separately, you can use the QuickTime Player to convert the sample rate to match that of your Final Cut Sequence, though lots of folks have used 44.1kHz audio successfully in FCE. (I think you need QT Pro in order to have the sample rate conversion capability.)
    As you begin recording, include a clap or some big sound to aid you in synching the two audio sources. Once you get everything recorded, I'd bring the video into your Timeline with the camera's audio, then import the external audio, synch up the audio sources, then unlink the camera audio from the video and delete that audio.

Maybe you are looking for

  • Export to EXCEL directly via SystemCommand?

    Hi! Im Using a SAP-Desktop-Link to login automatically, start a transaction, passing values to dynpro fields. That works Fine. The mylink.sap opened in an texteditor looks like this: SystemCommand=N*FBL3N -dynprofield1=foo;-dynprofield2=bar is there

  • ACCEPT

    Hi everybody! Does the ACCEPT command work in a stored procedure or in a compiled PL/SQL procedure??? URGENT!!!! Hope ur able to help me! thomas

  • How to convert frames into hours:minutes:seconds?

    if I have some columns which contains the time value in frames, i.e. Content_Duration, program_duration etc. How can I convert those frames in hh:mm:ss? Is it possible to do it with a function do that so that I can re-use it? Thanks

  • Win_api_directory_list in Forms 10G

    I have installed Forms 10G and it works very well, but when i try to use the win_api function win_api_directory_list.NextFile(hSearch, vcFile, bIsDir, FALSE); appear this oracle message error : ORA 6508 : PL/SQL: could not find program unit being cal

  • Auth. variable behavior on a report

    We've implemented HR Structural Authorization on 0ORGUNIT hierarchy. When a report is run with an authorization variable the Org Unit Hierarchy node is automatically populated with the highest allowable org.unit each and every time, even when just re