Results 21 to 31 of 31
- 08-04-2012, 10:34 PM #21
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.
You don't have any methods yet. There won't be any problem for the methods in the class to access the class variables.How do I referrence the instance variables from the methods I create in my Vehicle class?If you don't understand my response, don't ignore it, ask a question.
- 08-04-2012, 10:51 PM #22
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
Re: Please Help explain my assignment!!!
This is my Vehicle class, I dont even have the guts to compile it...
and this is what I have done so far with the main...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()); } }
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.
- 08-04-2012, 11:05 PM #23
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.
- 08-04-2012, 11:08 PM #24
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
Re: Please Help explain my assignment!!!
oh I thought that was a constructor
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:public Vehicle(String car) { type = car; }
Java Code:The type of vehicle is a car
- 08-04-2012, 11:18 PM #25
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
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
test.javaJava 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); } }
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.
- 08-05-2012, 01:00 AM #26
Re: Please Help explain my assignment!!!
Please post the full text of the error message.im getting an error when I compile test.java "cannot find symbol"
You need a constructor in the last posted code.
Hint: constructors do not have a return type and are not staticIf you don't understand my response, don't ignore it, ask a question.
- 08-05-2012, 01:11 AM #27
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
Re: Please Help explain my assignment!!!
never mind that error, and thank you, moving on. Question: I thought I would need to identify this>>>
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?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);Last edited by gnarkill10; 08-05-2012 at 01:20 AM. Reason: IGNORE cmd error!
- 08-05-2012, 01:26 AM #28
Re: Please Help explain my assignment!!!
I'm not sure what you are asking. Is main a method? What class is it in?the main is where the display text needs to go though.
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.
- 08-05-2012, 01:45 AM #29
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
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.
- 08-05-2012, 05:15 AM #30
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
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:
My working Test file: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; } } }
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(); } }
- 08-05-2012, 02:57 PM #31
Similar Threads
-
can someone explain what this exactly does?
By liluma in forum New To JavaReplies: 4Last Post: 08-21-2011, 07:58 PM -
Anyone can explain this?
By kazumahits in forum New To JavaReplies: 1Last Post: 03-08-2011, 02:03 AM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 01:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 07:22 AM -
Can somebody explain me this plz
By ccie007 in forum New To JavaReplies: 4Last Post: 05-20-2010, 07:47 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks