Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
Like Tree1Likes

Thread: Please Help explain my assignment!!!

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default Re: Please Help explain my assignment!!!

    Now add a constructor to the class using the order of values as shown in the code you posted from your main.

    when you have defined that you should be able to compile the main program and execute the 4 tatements you posted to see if that part works.

    How do I referrence the instance variables from the methods I create in my Vehicle class?
    You don't have any methods yet. There won't be any problem for the methods in the class to access the class variables.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    This is my Vehicle class, I dont even have the guts to compile it...
    Java Code:
    public class Vehicle  
    {
    	private String type;
    	private String make;
    	private String colour;
    
    	//Constructor method same name as the class
    	public Vehicle(String car){
    	  type = car;
    	}
    
    	public String getType(){
    		return type;
    	}
    	
    	public void text(){
    		System.out.println("The type of vehicle is a %s\n", getType());
    	}
    
    
    }
    and this is what I have done so far with the main...
    Java Code:
      public static void main(String[] args)
      {
    
        //make instances of different vehicles (these are the vehicle objects)
        Vehicle carVehicle = new Vehicle("car", "Ford", "red");
        Vehicle textObject = new Vehicle.text(); //Added this for testing, dont know if its right though, I know that I have to tell the computer that I want this specific method from this specific class.
    
        Vehicle truckVehicle = new Vehicle("truck", "Nissan", "green");
        Vehicle busVehicle = new Vehicle("bus", "Mercedes", "white");
        Vehicle motorBikeVehicle = new Vehicle("motorbike", "Honda", "silver");
    Last edited by gnarkill10; 08-04-2012 at 10:58 PM.

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default Re: Please Help explain my assignment!!!

    The class needs a constructor.

    text isn't a useful name for a method. A method name should be a verb describing what it does.

    What is the purpose of the code on line 6?
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    oh I thought that was a constructor
    Java Code:
    public Vehicle(String car)
    {      
      type = car;    
    }
    ok ill go way and do a little more research. Yeh the purpose of that code on line 6 is because I wanted to see if I could print out.
    Java Code:
    The type of vehicle is a car

  5. #25
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    Ok so I have got the following code however im getting an error when I compile test.java "cannot find symbol"

    Vehicle.java
    Java Code:
    public class Vehicle  
    {
    	private String type;
    	private String make;
    	private String colour;
    
    	//Constructor method same name as the class for type
    	public static void Vehicle(String type, String make, String colour)
    	{		
    		System.out.printf("The type of vehicle is a %s\n" + type);
    		System.out.printf("and the make is %s\n" + make);
    		System.out.printf("and the colour is %s\n" + colour);	
    	}
    }
    test.java
    Java Code:
    class test{
    	public static void main(String[] args)
    	{
    		Vehicle carVehicle = new Vehicle("car", "Ford", "red");
    	}
    }
    Last edited by gnarkill10; 08-05-2012 at 12:29 AM.

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default Re: Please Help explain my assignment!!!

    im getting an error when I compile test.java "cannot find symbol"
    Please post the full text of the error message.

    You need a constructor in the last posted code.
    Hint: constructors do not have a return type and are not static
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    never mind that error, and thank you, moving on. Question: I thought I would need to identify this>>>
    Java Code:
    System.out.printf("The type of vehicle is a %s\n" + type);        
    System.out.printf("and the make is %s\n" + make);        
    System.out.printf("and the colour is %s\n" + colour);
    Within the Vehicle.java class. Because the main was given to us I thought we would not be able to edit it, Im pretty sure that the main is where the display text needs to go though. Is this right?
    Attached Thumbnails Attached Thumbnails Please Help explain my assignment!!!-cmd.png  
    Last edited by gnarkill10; 08-05-2012 at 01:20 AM. Reason: IGNORE cmd error!

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default Re: Please Help explain my assignment!!!

    the main is where the display text needs to go though.
    I'm not sure what you are asking. Is main a method? What class is it in?

    So show the contents of the Vehicle class, I'd add a method that prints out the contents of the class's variables.

    Instead of an image, copy and paste the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'
    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    woooh its working, wow I need sleep!!! It was basically just a syntax error. Derrrr.

    I just need to work out how to implement the Insertion sort now. XD so excited/mega tired, must not sleep. Coffee time.
    Last edited by gnarkill10; 08-05-2012 at 02:01 AM.

  10. #30
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Please Help explain my assignment!!!

    Having problems with my insertion sort, I have the insertion sort working with numbers. However stuck on how I should implement the strings from my code. Also where I need to include the compareTo method.

    My working code for insertion sort:

    Java Code:
    /**
       The IntInsertionSorter class provides a public static
       method for performing an insertion sort on an int array.
    */
    
    public class insertionSort
    {
    
       /**
          The insertionSort method performs an insertion sort on
          an int array. The array is sorted in ascending order.
          @param array The array to sort.
       */
    
       public static void insertionSort(int[] array)
       {
          int unsortedValue;  // The first unsorted value
          int scan;           // Used to scan the array
          
          // The outer loop steps the index variable through 
          // each subscript in the array, starting at 1. This
          // is because element 0 is considered already sorted.
          for (int index = 1; index < array.length; index++)
          {
             // The first element outside the sorted subset is
             // array[index]. Store the value of this element
             // in unsortedValue.
             unsortedValue = array[index];
             
             // Start scan at the subscript of the first element
             // outside the sorted subset.
             scan = index;
             
             // Move the first element outside the sorted subset
             // into its proper position within the sorted subset.
             while (scan > 0 && array[scan-1] > unsortedValue)
             {
                array[scan] = array[scan - 1];
                scan--;
             }
             
             // Insert the unsorted value in its proper position
             // within the sorted subset.
             array[scan] = unsortedValue;
          }
       }
    }
    My working Test file:

    Java Code:
    /**
       This program tests the insertionSort method in the
       IntInsertionSorter class.
    */
    
    public class insertionSortTest
    {
       public static void main(String[] args)
       {
          // Create an int array with test values.
          int[] values = { 5, 1, 3, 6, 4, 2 };
          
          // Display the array's contents.
          System.out.println("Original order: ");
          for (int element : values)
             System.out.print(element + " ");
          
          // Sort the array.
          insertionSort.insertionSort(values);
    
          // Display the array's contents.
          System.out.println("\nSorted order: ");
          for (int element : values)
             System.out.print(element + " ");
             
          System.out.println();
       }
    }

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default Re: Please Help explain my assignment!!!

    Did you do the experiments with the compareTo() method to see how it worked?
    The look at how to use compareTo() with two Strings vs > with two ints.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. can someone explain what this exactly does?
    By liluma in forum New To Java
    Replies: 4
    Last Post: 08-21-2011, 07:58 PM
  2. Anyone can explain this?
    By kazumahits in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:03 AM
  3. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  5. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 07:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •