Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   My test question (https://www.askmehelpdesk.com/showthread.php?t=737197)

  • Mar 3, 2013, 01:32 PM
    kaka1994
    my test question
    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.
  • Mar 3, 2013, 01:41 PM
    kaka1994
    challenging program part(B)..
    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.
  • Mar 3, 2013, 02:12 PM
    Curlyben
    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..
  • Mar 4, 2013, 07:18 AM
    kaka1994
    Quote:

    Originally Posted by kaka1994 View Post
    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;
    }
    }
  • Mar 4, 2013, 07:19 AM
    kaka1994
    [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

  • All times are GMT -7. The time now is 11:07 PM.