PDA

View Full Version : My test question


kaka1994
Mar 3, 2013, 01:32 PM
Write a Java class called Car that has three instance variables: (10 points)
make of type String to represent the make of the car (e.g. Mercedes, Opel, Toyota, etc.),
year of type int represents the year of production
price of type float represents the price of the car.
1) Your class must include
a) A no argument constructor that assigns an empty string to the make and zeros to the
year of production and price.
b) A constructor that initializes each variable to an appropriate value passed as an
argument to the constructor.
c) A setter and getter methods for setting and getting the values of make variable only.
2) Write a test program that
a) creates a Car object with make=”Jaguar”, year=2011, and price= 20000.
b) Change value of the make variable for the object created in part a to Mercedes
c) Prints all values stored in the fields of the objected created above.

kaka1994
Mar 3, 2013, 01:41 PM
A. The Java program below is intended to sort an array of String objects. Complete the program by implementing the selectionSort function for arrays. A demo program from chapter 8 is provided with the assignment as an example of how to implement selection sort for an array of integers.

public static void main(String[] args)
{
String[] names = {"Joseph", "Aya", "Sally",
"Mohammad", "Ragda", "Bayan"};
// Display the unsorted array.
System.out.println("The unsorted values are:");
for (int I = 0; I < names.length; I++)
System.out.println(names[i] + " ");
System.out.println();

// Sort the array.
selectionSort(names);

// Display the sorted array.
System.out.println("The sorted values are:");
for (int I = 0; I < names.length; I++)
System.out.println(names[i] + " ");
}


B. Write a program to sort a list of names read from a text file. One challenge with this program is to determine the number of names in a file. I wrote a function called fileLineCount( ) to do this. I tested it in a file called FileLineCountDemo.java This program should give you some clues about reading the names from a file into an array. Once that is accomplished, the rest follows along the lines of part A.

Curlyben
Mar 3, 2013, 02:12 PM
What do YOU think ?
While we're happy to HELP we won't do all the work for you.
Show us what you have done and where you are having problems..

kaka1994
Mar 4, 2013, 07:18 AM
A. The Java program below is intended to sort an array of String objects. Complete the program by implementing the selectionSort function for arrays. A demo program from chapter 8 is provided with the assignment as an example of how to implement selection sort for an array of integers.

public static void main(String[] args)
{
String[] names = {"Joseph", "Aya", "Sally",
"Mohammad", "Ragda", "Bayan"};
// Display the unsorted array.
System.out.println("The unsorted values are:");
for (int i = 0; i < names.length; i++)
System.out.println(names[i] + " ");
System.out.println();

// Sort the array.
selectionSort(names);

// Display the sorted array.
System.out.println("The sorted values are:");
for (int i = 0; i < names.length; i++)
System.out.println(names[i] + " ");
}


B. Write a program to sort a list of names read from a text file. One challenge with this program is to determine the number of names in a file.
I have done part A help but I don't know how to do part B
part A :
public static void main(String[] args)
{
String[] names = {"Joseph", "Aya", "Sally",
"Mohammad", "Ragda", "Bayan"};
// Display the unsorted array.
System.out.println("The unsorted values are:");
for (int I = 0; I < names.length; I++) {
System.out.println(names[i] + " ");
}
System.out.println();

// Sort the array.
selectionSort(names);

// Display the sorted array.
System.out.println("The sorted values are:");
for (int I = 0; I < names.length; I++) {
System.out.println(names[i] + " ");
}
System.out.println();
}
static void selectionSort(Comparable[] names)
{
int smallindex;
for(int I = 0; I < names.length; I++)
{
smallindex = I; // set first element as smallest
for(int j = I + 1; j < names.length; j++) {
if(names[j].compareTo(names[smallindex]) < 0) {
smallindex = j;
}
}
if(smallindex != I) {
swap(names, smallindex, I);
}
}
}


// Swap for two elements of an array of Objects

//index1 the index of the first element to swap
// index2 the index of the second element to swap

static void swap(Object[] names, int index1, int index2)
{
Object temp = names[index1];
names[index1] = names[index2];
names[index2] = temp;
}
}

kaka1994
Mar 4, 2013, 07:19 AM
[QUOTE=Curlyben;3410247]What do YOU think ?
While we're happy to HELP we won't do all the work for you.
Show us what you have done and where you are having problems..

I'm sorry.. please help me with part B