Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package com.example.test; import android.os . Bundle; import androidx.appcompat.app.AppCompatActivity; import com.chaquo.python.PyObject; import com.chaquo.python.Python; import com.chaquo.python.android.AndroidPlatform; import java.util.Arrays; import java.util.Comparator; public class MainActivity extends AppCompatActivity { @Override
package com.example.test;
import android.os
Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import java.util.Arrays;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate
Bundle savedInstanceState
super.onCreate
savedInstanceState
;
setContentView
R
layout.activity
main
;
if
Python.isStarted
Python.start
new AndroidPlatform
this
;
Python py
PythongetInstance
;
PyObject module
py
getModule
myhelper
;
PyObject findGasStations
moduleget
find
gas
stations"
;
PyObject result
findGasStationscall
;
PyObject
pyArray
resultasList
toArray
new PyObject
;
Convert PyObject array to Java array of GasStation objects
GasStation
gasStations
new GasStation
pyArray
length
;
for
int i
; i
pyArraylength; i
PyObject pyGasStation
pyArray
i
;
Extract attributes from the PyObject representing a GasStation
String name
pyGasStationget
name
toString
;
String location
pyGasStationget
location
toString
;
Extract prices and print them
double regularPrice
pyGasStationget
regular
price"
toDouble
;
double midgradePrice
pyGasStationget
midgrade
price"
toDouble
;
double premiumPrice
pyGasStationget
premium
price"
toDouble
;
System
out.println
Regular Price:
regularPrice
;
System
out.println
Midgrade Price:
midgradePrice
;
System
out.println
Premium Price:
premiumPrice
;
String rating
pyGasStationget
rating
toString
;
:
Create a new GasStation object and add it to the array
gasStations
i
new GasStation
regularPrice
midgradePrice premiumPrice, name, location, rating
;
Sort the gas stations based on regular prices
gasStations
SortRegGas
gasStations
;
Print the sorted gas stations
for
GasStation station : gasStations
System.out.println
station
toString
;
Function to sort gas stations based on regular prices
private GasStation
SortRegGas
GasStation
gasStations
Arrays.sort
gasStations
station
station
Doublecompare
station
getRegularPrice
station
getRegularPrice
;
return gasStations;
Make functions that take in the gas station class array as a variable
function sorts based on cheapest regular gas, the next on cheapest mid grade and the last on cheapest premium
The function should return the array
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started