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 2 of 2

Thread: Basic Java

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Basic Java

    If given these questions:

    a. Add an instance variable to hold the width (thickness) of the line. You may implement the width as an
    int. Then add 2 access methods for this color variable: a) set the width for a Line. b) get a Line's width.
    b. Add an instance variable to hold the color of the line. For this assignment, the data type of the color
    could be an int value or a String (Later we will talk about a Color class but it is not required in this
    assignment). Then add 2 access methods for this color variable: a) set the color for a Line. b) get a
    Line's color.
    c. Calculate and return (get) the length of a Line. Add a method for calculating the length based on its
    coordinates. The method returns a double. The formula is sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)).
    You can use the java.lang.Math class for calculating the square root (The method is sqrt(), you need to
    check the API doc for its usage). Do not add an instance variable to hold the length.


    For this code:

    /************************************************** ***************
    This program demonstrates a simple "Line" class.
    Here, a Line class is defined with its properties and
    interface (i.e., its methods).
    A main class then creates instances of this Line class
    and calls on the methods to demonstrate its behavior.
    ************************************************** ***************/
    import java.io.*;
    public class Line
    {
    private int x1, y1, x2, y2; //coordinates of the line

    //Constructor
    //Receives 4 integers which are the Line's start and end points.

    publc width( )

    public Line(int left, int top, int right, int bottom)
    {
    // each of these validates its argument - see below.
    setLeft(left);
    setTop(top);
    setRight(right);
    setBottom(bottom);
    } // end constructor
    //*************************************
    //method draw() calls another method called drawLine(),
    //which is assumed to be a graphics primitive on the
    //system. However, since this program will be
    //run in console mode, a text description of the Line
    //will be displayed.
    //
    public void draw()
    {
    drawLine(x1, y1, x2, y2);
    }

    //*************************************
    //method drawLine() simulates drawing of a line for console mode.
    //It should describe all the important attributes of the line.
    //In a graphics mode program, we would delete this and use the
    //system's Graphics library drawLine().
    //
    private void drawLine(int x1, int y1, int x2, int y2)
    {
    System.out.println("Draw a line from x of " + x1 + " and y of " + y1);
    System.out.println("to x of " + x2 + " and y of " + y2 + "\n");
    }

    //*************************************
    //Method setLine() allows user to change the points of the
    //already existing Line.
    //
    public void setLine(int left, int top, int right, int bottom)
    {
    setLeft(left);
    setTop(top);
    setRight(right);
    setBottom(bottom);
    }

    // -- the individual setXXXX methods that prevent
    // any line's coordinate from being offscreen.
    // In the event of an invalid (offscreen) value,
    // that value is (silently) set to 0.
    //**************************
    public void setLeft(int left)
    {
    if (left < 0 || left > 639)
    x1 = 0;
    else
    x1 = left;
    }
    //**************************
    public void setTop(int top)
    {
    if (top < 0 || top > 479)
    y1 = 0;
    else
    y1 = top;
    }
    //**************************
    public void setRight(int right)
    {
    if (right > 639 || right < 0)
    x2 = 0;
    else
    x2 = right;
    }
    //**************************
    public void setBottom(int bottom)
    {
    if (bottom > 479 || bottom < 0)
    y2 = 0;
    else
    y2 = bottom;
    }
    //Now for some "get" Access methods to get individual values
    //**************************
    public int getLeft()
    {
    return x1;
    }

    //**************************
    public int getTop()
    {
    return y1;
    }

    //**************************

    public int getRight()
    {
    return x2;
    }

    //**************************
    public int getBottom()
    {
    return y2;
    }
    } // end class Line


    How would you go about answering these questions.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Basic Java

    I assume its your homework, so I'll let you make the first pass on answering the questions and explaining why you think those answers are correct.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java basic Example
    By Appu14 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 3rd, 2012, 11:24 AM
  2. [SOLVED] Basic Java Program Help
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: January 18th, 2012, 12:47 AM
  3. Re: Basic Java Program Help
    By Tanushri in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2012, 12:43 AM
  4. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 05:24 PM
  5. Few very basic Java questions.
    By 01001010 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 13th, 2010, 01:14 PM