Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree1Likes

Thread: Please Help explain my assignment!!!

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

    Default Please Help explain my assignment!!!

    Can someone please help me with clarification of my assignment, it was due yesterday. I have been reading up on it all week, and I just don't know how to start it, my lecturer is just not responding to any of the emails I sent at the stay of the week.

    I have to make a program that sorts strings using the insertion sort.

    Basically the program is for a vehicle class, this is the superclass. The subclasses are car, motorbike, truck, and bus.

    I need to sort them alphabetically using the type, make, and color.

    This is how the output should look, might explain a bit better.

    % java VehicleTest

    Thank you,

    Ben

    Output of array before being sorted
    The TYPE of vehicle is a CAR and the MAKE is FORD and the COLOR is RED
    The TYPE of vehicle is a TRUCK and the MAKE is NISSAN and the COLOR is GREEN
    The TYPE of vehicle is a BUS and the MAKE is MERCEDES and the COLOR is WHITE
    The TYPE of vehicle is a MOTORBiKE and the MAKE is HONDA and the COLOR is SILVER

    Output of array after being sorted by vehicle MAKE
    The TYPE of vehicle is a CAR and the MAKE is FORD and the COLOR is RED
    The TYPE of vehicle is a MOTORBiKE and the MAKE is HONDA and the COLOR is SILVER
    The TYPE of vehicle is a BUS and the MAKE is MERCEDES and the COLOR is WHITE
    The TYPE of vehicle is a TRUCK and the MAKE is NISSAN and the COLOR is GREEN

    Output of array after being sorted by vehicle TYPE
    The TYPE of vehicle is a BUS and the MAKE is MERCEDES and the COLOR is WHITE
    The TYPE of vehicle is a CAR and the MAKE is FORD and the COLOR is RED
    The TYPE of vehicle is a MOTORBiKE and the MAKE is HONDA and the COLOR is SILVER
    The TYPE of vehicle is a TRUCK and the MAKE is NISSAN and the COLOR is GREEN

    Output of array after being sorted by vehicle COLOR
    The TYPE of vehicle is a TRUCK and the MAKE is NISSAN and the COLOR is GREEN
    The TYPE of vehicle is a CAR and the MAKE is FORD and the COLOR is RED
    The TYPE of vehicle is a MOTORBiKE and the MAKE is HONDA and the COLOR is SILVER
    The TYPE of vehicle is a BUS and the MAKE is MERCEDES and the COLOR is WHITE


    %program finished

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    Which part of the assignment are you having trouble understanding? It seems very straight forward.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    Thank you.
    Firstly I'm having trouble understanding how to sort the strings using insertion sort alphabetically, I don't know whether I tell the program to sort determined on the first letter, I'm pretty sure I have to use the compare to method.
    Basically I'm just really really confused, I'm stressing mega, I have been reading my lecture notes and textbooks, but I don't really know how to go about it (start it). If I send the assignment task do you think maybe you could explain it simply to me, that would be a MASSIVE help, I would really appreciate it. Maybe something along the lines of pseudo code, but not giving me the answer because I want to do it myself.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    Do you have the algorithm for the insertion sort? If not Google it.
    When you get the algorithm, try writing pseudo code for it. If you have problems, copy the algorithm's text and the pseudo code here with your questions.
    Once you have pseudo code you can start writing code.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    Norm you are so speedy with your responses, it really is great to know there are still people like you.

    I dont really want to overload the page but feel if I post more info for you, you might understand my problem.

    ************************************************** ***************************************
    The Assignment:

    Objectives:
    This assignment aims to use Java skills or concepts including:
    •Aggregation
    •Methods
    •Sorting Algorithm
    •Input/Output Expressions
    •Selection
    •Repetition

    **VEHICLE ASSIGNMENT**
    In this assignment you will need to design and implement a class that represents a vehicle (e.g., car, truck, bus etc). The class should contain the following information (all represented by String class)
    1.type
    2.make
    3.colour
    The Vehicle class should also contain methods to allow a Vehicle object to be constructed, its fields to be set and retrieved (i.e., set and get methods for each of its fields), as well as a toString() method.
    The program will create objects of the Vehicle class and store them in a Vehicle array structure.

    Once the Vehicle objects have been stored in a Vehicle array structure, the program will allow objects in the array to be sorted in either of the following ways.
    1.by type OR
    2.by make OR
    3.by colour
    The sorting algorithm to be used is the insertion sort.


    Example of the program in execution:


    % java VehicleTest

    Output of array before being sorted
    The type of vehicle is a car and the make is Ford and the colour is red
    The type of vehicle is a truck and the make is Nissan and the colour is green
    The type of vehicle is a bus and the make is Mercedes and the colour is white
    The type of vehicle is a motorbike and the make is Honda and the colour is silver

    Output of array after being sorted by vehicle make
    The type of vehicle is a car and the make is Ford and the colour is red
    The type of vehicle is a motorbike and the make is Honda and the colour is silver
    The type of vehicle is a bus and the make is Mercedes and the colour is white
    The type of vehicle is a truck and the make is Nissan and the colour is green

    Output of array after being sorted by vehicle type
    The type of vehicle is a bus and the make is Mercedes and the colour is white
    The type of vehicle is a car and the make is Ford and the colour is red
    The type of vehicle is a motorbike and the make is Honda and the colour is silver
    The type of vehicle is a truck and the make is Nissan and the colour is green

    Output of array after being sorted by colour
    The type of vehicle is a truck and the make is Nissan and the colour is green
    The type of vehicle is a car and the make is Ford and the colour is red
    The type of vehicle is a motorbike and the make is Honda and the colour is silver
    The type of vehicle is a bus and the make is Mercedes and the colour is white
    %



    The Program:
    The program will be composed of 3 files:
    1.Vehicle.java
    2.VehicleTest.java
    3.VehicleObjectInsertionSorter.java

    The VehicleTest.java is provided for you (I will paste this into another reply)

    To sort the array of Vehicles you will need to implement the VehicleObjectInsertionSorter class, which will need to contain the following:
    •A method named insertionSort
    •insertionSort(Vehicle[] array, String field) ◦accepts an array of Vehicle and a
    ◦String object specifying whether we are to sort the array either by:
    1.make OR
    2.type OR
    3.colour

    ◦the sort method should be public, static and contain a void return statement





    Getting Started:
    The application file VehicleTest.java is provided to help get you started:
    Also, you should make use of your solution to Task 2 of Lab_02 - i.e,. use your solution for the ObjectInsertionSorter class to implement VehicleObjectInsertionSorter class.



    Important Notes
    •For the purposes of this assignment you are NOT allowed to use the ArrayList class as part of your solution.
    •It is necessary to format the output so that it displays as given in the example.
    •Test your program on turing - that is where it will be marked.
    •You must comment your program well. The program must be readable as well (e.g., spacing, indetation etc).



    Submitting This Assignment

    Your assignment must be submitted using the submit program


    •This assignment is assignment a2
    •You will need to submit three files for this assignment:
    1.An algorithm file which will contain the algorithm for the sorting method
    It should be in plain text files named algorithm.txt
    2.The Vehicle.java (class) file containing the specified methods
    It should be in plain text files named Vehicle.java
    3.The VehicleObjectInsertionSorter class containing the specified sorting method.
    It should be in plain text files named VehicleObjectInsertionSorter.java

    Your submission should also include a script showing your program being compiled and executed (tested).

  6. #6
    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 the main that was given to us by the lecturer

    ************************************************** ************************************************** ******

    public class VehicleTest
    {
    public static int MAX = 4;
    public static void main(String[] args)
    {

    //make instances of different vehicles
    Vehicle carVehicle = new Vehicle("car", "Ford", "red");
    Vehicle truckVehicle = new Vehicle("truck", "Nissan", "green");
    Vehicle busVehicle = new Vehicle("bus", "Mercedes", "white");
    Vehicle motorBikeVehicle = new Vehicle("motorbike", "Honda", "silver");

    System.out.println("");

    //create an array of these Vehicles
    Vehicle[] ve = new Vehicle[MAX];
    ve[0] = carVehicle;
    ve[1] = truckVehicle;
    ve[2] = busVehicle;
    ve[3] = motorBikeVehicle;

    System.out.println("\nOutput of array before being sorted");

    for(int i = 0; i < MAX; i++)
    System.out.println(ve[i]);

    // Sort the array by vehicle make.
    VehicleObjectInsertionSorter.insertionSort(ve,"mak e");

    System.out.println("\nOutput of array after being sorted by vehicle make");
    for(int i = 0; i < MAX; i++)
    System.out.println(ve[i]);

    // Sort the array by vehicle type.
    VehicleObjectInsertionSorter.insertionSort(ve,"typ e");

    System.out.println("\nOutput of array after being sorted by vehicle type");
    for(int i = 0; i < MAX; i++)
    System.out.println(ve[i]);

    // Sort the array by vehicle colour.
    VehicleObjectInsertionSorter.insertionSort(ve,"col our");

    System.out.println("\nOutput of array after being sorted by vehicle colour");
    for(int i = 0; i < MAX; i++)
    System.out.println(ve[i]);

    }
    }
    ************************************************** ************************************************** ***************

  7. #7
    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 all the info I have on the insertion sort, as provided by lecturer

    Insertion Sort
    •Insertion Sort works by extending the length of the sorted portion one step at a time:
    •it takes the first element and considers it to be sorted (trivially)
    •it then looks at the second element and compares it to the first.
    •it compares them and swaps them if necessary - now the array has 2 sorted elements. ◦ this now becomes the sorted portion of the array

    •next the 3rd element of the array is moved into the correct position in the sorted part of the array
    •this continues until all the array elements have been processed.
    •the code below implements this process /**
    The IntInsertionSorter class provides a public static
    method for performing an insertion sort on an int array.
    */

    public class IntInsertionSorter
    {

    /**
    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;
    }
    }
    }

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

    Default Re: Please Help explain my assignment!!!

    Far out im going to give you a heart attack with all that reading. I dont really know what I need to do to start. In the Vehicle Class (Vehicle.java) do I just need the insertion sort algorithm, surely its not that easy...
    Last edited by gnarkill10; 08-04-2012 at 02:54 PM. Reason: gramma, left out info

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Please Help explain my assignment!!!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    I'm not going to read through the assignment and tell you how to do it. I'm waiting for specific questions about where you are having problems.

    I suggest that you do the program one step at a time.
    Read in the input and create a collection of the vehicles.
    Print out the collection to see what is there.
    Then work on the sorts.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    Thanks Norm, I dont expect you to tell me how to do it. How will I learn this way, I only posted all the info I had for you to understand.
    I dont have specific questions, cause I dont know how to start.

    What you have written has been some help, thank you. I guess I was just after a little direction.
    I suggest that you do the program one step at a time.
    Read in the input and create a collection of the vehicles.
    Print out the collection to see what is there.
    Then work on the sorts.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    Where is the vehicle data the program is to work with? If it is in a file, start with a program too read in the data in the file and print it out to see that the reading is done correctly.

    After that works move on to creating Vehicle objects from that data and saving them.
    When that is done, print out the list of vehicles that have been saved.
    gnarkill10 likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    The only vehicle data to be used is the data given in the main program VehicleTest see below

    //make instances of different vehicles
    Vehicle carVehicle = new Vehicle("car", "Ford", "red");
    Vehicle truckVehicle = new Vehicle("truck", "Nissan", "green");
    Vehicle busVehicle = new Vehicle("bus", "Mercedes", "white");
    Vehicle motorBikeVehicle = new Vehicle("motorbike", "Honda", "silver");

    If it is in a file, start with a program too read in the data in the file and print it out to see that the reading is done correctly.
    Ah yes I get what you're saying, like this you mean?
    XML Code:
    File myFile = new File("thisIsYourFile.txt"); 
    Scanner inputFile = new Scanner(myFile);
    Ahh true, dont know how I missed that, ok so I need to create my objects for the Vehicles in the Vehicle.java class. They are already refferenced in my main as instance variables, but because I havent created the objects the program is saying "Vehicle?? What the hell are you talking about, are you on smack? I cant find no vehicle objects"
    XML Code:
    //make instances of different vehicles
     Vehicle carVehicle = new Vehicle("car", "Ford", "red");
     Vehicle truckVehicle = new Vehicle("truck", "Nissan", "green");
     Vehicle busVehicle = new Vehicle("bus", "Mercedes", "white");
     Vehicle motorBikeVehicle = new Vehicle("motorbike", "Honda", "silver");
    Ok cool, I guess I just thinking too much.

    Again man, thank you sooo much. Legend.

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

    Default Re: Please Help explain my assignment!!!

    I think I have some specific questions now, if you can answer without giving away too much detail.

    1.Using the compare to method, no doubt I will be. They are not Integers so therefore I will have to compare characters based on alphabetical order, is this right?
    2.If comparing on the basis of Alphabetical order, do I/can I compare the whole string?
    For example I have a bunch of names all starting with N.
    XML Code:
         Unsorted list of names:<BR />
         <BR />
         Norm
         Neville
         Neil
         Nathan
         Nigel
         Nicholas
         <BR />
         Sorted list of names:<BR />
         <BR />
         Nathan
         Neil
         Neville
         Nicholas
         Nigel
         Norm
    **silly break statements why wont you work!**

    As you can see the names above all start with N, so then the program looks at the next character and compares this.

    This is what I want to implement in my program, can I do this. (yes or no is fine, or maybe if you could tell me what to search on google for this type of method).
    Last edited by gnarkill10; 08-04-2012 at 05:44 PM.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    The String class's compareTo() method will help you sort Strings. You won't need to look at the String char by char
    To see how the method works and what value it returns, write a small simple program that uses the compareTo() method and prints out the results:
    System.out.println("first".compareTo("next"));

    Code several of the above with different Strings where first and next are to see what the method does.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    So ahh yep im still lost

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    Have you tried the compareTo() method to see how it works? You'll need it for the sort.

    Can you explain what you have now (for example what the instructor gave you) and what you need to write to finish the assignment?

    You need to break the job up into a list of simple steps and do it one step at a time.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    Im stuck on this, the very first step, ahh gosh!

    "In this assignment you will need to design and implement a class that represents a vehicle (e.g., car, truck, bus etc). The class should contain the following information (all represented by String class)
    1.type
    2.make
    3.colour "

    I understand that car, bus, motorbike, and truck are all subclasses of Vehicle, but do I need to worry about this? In the methods I need to create within the Vehicle.java class should I be using inheritence?
    XML Code:
    public class Car extends Vehicle{
    		
    	}
    I am its doing my head in!!!

    Also if the type, make, and colour of vehicle has been refferenced in main (instance variables), I dont even know what my question is. I know I need to use something like this

    XML Code:
    public String toString(){
    		return String.format ("The type of vehicle is a" %s "and the make is" %s and the colour is %s");
    	}
    Ahhhhhhh I don't know what to do

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Please Help explain my assignment!!!

    Start with the first steps: define the class and add 3 variables to it as per:
    "In this assignment you will need to design and implement a class that represents a vehicle (e.g., car, truck, bus etc). The class should contain the following information (all represented by String class)
    1.type
    2.make
    3.colour "
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Please Help explain my assignment!!!

    I feel like this isnt correct somehow...

    Java Code:
    public class Vehicle  
    {
    	private String type;
    	private String make;
    	private String colour;
    
    
    
    }
    My question is : How do I referrence the instance variables from the methods I create in my Vehicle class? I keep thinking that it wont work, like I need individual refferences because their are multiple objects. This is not making sense!

    this is the code from my main:
    Java Code:
    Vehicle carVehicle = new Vehicle("car", "Ford", "red");
              Vehicle truckVehicle = new Vehicle("truck", "Nissan", "green");
              Vehicle busVehicle = new Vehicle("bus", "Mercedes", "white");
              Vehicle motorBikeVehicle = new Vehicle("motorbike", "Honda", "silver");
    I understand these are instance variables or objects, but I cannot use these until I create the methods in my Vehicle class. Basically this is where I am having most of my trouble. Not knowing how to implement this.
    Last edited by gnarkill10; 08-04-2012 at 10:23 PM.

Page 1 of 2 12 LastLast

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
  •