Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a program that reads a text file and stores the data into an Object calle

ID: 3550651 • Letter: I

Question

I need a program that reads a text file and stores the data into an Object called fruit

Say the text file data is as follows for example:

Apples,9,10.50,joe

Bananas, 15, 11.00,crystal

Cranberry, 2, 9.00, sara

Dragonfruit,5,13.00, amber



Okay now as you can see the first index is already sorted then I need to use a quicksort algorithm to sort the second index which is for instance quantiy and then third index price by descending order and customer by alphabet. These should all be put into an Object called fruit and the text file should be called for instance fruit.java.

Could you help me get started on this please? its not the assignment but I made up something small that I can figure the rest out from. Thanks

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;


class fruits{

String name;

int quantity;

float price;

String customer;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getQuantity() {

return quantity;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}

public String getCustomer() {

return customer;

}

public void setCustomer(String type) {

this.customer = type;

}

}

public class fruit {

public static void quickSort(int[] a, int p, int r)

{

if(p<r)

{

int q=partition(a,p,r);

quickSort(a,p,q);

quickSort(a,q+1,r);

}

}


private static int partition(int[] a, int p, int r) {


int x = a[p];

int i = p-1 ;

int j = r+1 ;


while (true) {

i++;

while ( i< r && a[i] < x)

i++;

j--;

while (j>p && a[j] > x)

j--;


if (i < j)

swap(a, i, j);

else

return j;

}

}


private static void swap(int[] a, int i, int j) {

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}

public static void main(String args[])

{

ArrayList<fruits> arr=new ArrayList<fruits>();

fruits fr=new fruits();

Scanner sc2 = null;

try {

sc2 = new Scanner(new File("fruit.txt"));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

while (sc2.hasNextLine()) {

Scanner s2 = new Scanner(sc2.nextLine());

  

String s = s2.next();

fr.setName(s);

int i=Integer.parseInt(s2.next());

fr.setQuantity(i);

float f=Float.parseFloat(s2.next());

fr.setPrice(f);

s=s2.next();

fr.setCustomer(s);

arr.add(fr);

}

  

/*Now you have the arraylist of fruits objects.

Take each object and create an array of the values that you want to sort

based on index and pass that array to the quick sort method*/

  

//sort based on second index: START

int quant[]=new int[10];

for(int i=0;i<arr.size();i++)

{

fr=arr.get(i);

int q=fr.getQuantity();

quant[i]=q;

}

//call quick sort for the array

quickSort(quant,0,quant.length);

//sort based on second index: END

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote