Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Is My answers correct??

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Is My answers correct??

    Good day every body,

    In my last exam in Java, the teacher gave me a ready code and asked to find ten erron in it.

    the code and my anseres as underlined below:

    Public Class box 
     
    {
    [COLOR="Red"]x int [] [/COLOR]= new [COLOR="red"]int (5)[/COLOR];
    int y [] = new int [10];
    [COLOR="red"]length double;[/COLOR]
    int width;
    private static double f = 10 [COLOR="red"]// missing ;[/COLOR]
     
    Box obj1 = new [COLOR="red"]XYZ ()[/COLOR];
    [COLOR="red"]public box();[/COLOR]
     
    public Box (double len, int wid, double f)
    { 
    length = len;
    width = wid;
    [COLOR="red"]f = ‘a’;[/COLOR]
    }
    public [COLOR="red"]int [/COLOR]setLength (int l);[COLOR="red"] //no ;[/COLOR]
    {
    length = l;  
    }
    } [COLOR="red"]// extra braces[/COLOR]
    } //end Box class

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

    the teacher minus me one mark,

    Who is correct??


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Is My answers correct??

    Hi Java.Coder(),

    The things above are quite confusing. What are the things in red? Are they your answers? Or they are the actual errors? Or something else?

    Can you simply post the exact code that your teacher gave you? Don't overwrite your answers in the same code, put them as a comment on a separate line.

    For e.g.
    length double;
    //double length;
    where the portion after // is what you have corrected.

    In short, properly format your question, so that it would be easy for us to understand it.

    Plus post your code in the similar fashion as I have posted above using [highlight=java] Your Java code [/*highlight] (Remove the star[*] in closing bracket when you use it).

    Hope you are clear with this.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Is My answers correct??

    thank you goldes for your reply

    the things in red is my answere, since he just asked to underline the error without any correction or justification

     
    Public Class box 
     
    {
    x int [] = new int (5); 
    // int [] = new int[5]; -- two errors
    int y [] = new int [10];
    length double;
    // double length
    int width;
    private static double f = 10 
    // ; missing
    Box obj1 = new XYZ ();
    // Box obj1 = new Box(); -- constructor name
    public box();
    // error - 
    public Box (double len, int wid, double f)
    { 
    length = len;
    width = wid;
    f = ‘a’;
    //f is double not char
    }
    public int setLength (int l); 
    // public void setLength(int l)  -- two errors
    {
    length = l;  
    }
    } 
    // extra bracket
    }

    I think the mentioned ten errors above is enough to get the full mark for this question

  4. #4
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Is My answers correct??

    Well,

    Frankly speaking we can assign a char into a double. The complier will not complain for that. It treats it as a ASCII value and prints it out easily.

    Look at the below code,
    public class Box {
    public static void main(String args[]) {
    double d = 'x';
    System.out.println("ASCII for small X: " + d);
     
    int i = 'X';
    System.out.println("ASCII for capital X: " + i);
    }
    }
    When you run this, it will print,
    ASCII for small X: 120.0
    ASCII for capital X: 88
    The real gotcha in your code is the first line itself. The first line is violating the keywords of java in a wrong way. What you have mentioned is "Public Class box". This is NOT allowed. Public and Class are NOT keywords in java, they are in fact "public" and "class" [Notice the small p and small c]

    So your error of assigning char into double is not actually an error, instead the first line itself is an error. Even the name of the class should be Box not box [notice capital B].

    I hope now you know where you lost one mark.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  5. #5
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default Re: Is My answers correct??

    Here is a link in case if you are interested to know more about ASCII: ASCII-Table

    This describes chars and their corresponding values in different formats.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Is My answers correct??

    Many thanks and regards goldest

  7. #7
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Is My answers correct??

    You are Welcome!
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  8. The Following User Says Thank You to goldest For This Useful Post:

    Java.Coder() (December 29th, 2010)

Similar Threads

  1. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM
  2. It's not printing out the correct percentage...
    By JBow94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2010, 04:07 PM
  3. Crude method of using offset whilst using BufferedReader - Correct way?
    By Pr0ject-Rec0n in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 9th, 2010, 09:59 PM
  4. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM