Answered step by step
Verified Expert Solution
Link Copied!

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
=
Python.getInstance
(
)
;
PyObject module
=
py
.
getModule
(
"
myhelper
"
)
;
PyObject findGasStations
=
module.get
(
"
find
_
gas
_
stations"
)
;
PyObject result
=
findGasStations.call
(
)
;
PyObject
[
]
pyArray
=
result.asList
(
)
.
toArray
(
new PyObject
[
0
]
)
;
/
/
Convert PyObject array to Java array of GasStation objects
GasStation
[
]
gasStations
=
new GasStation
[
pyArray
.
length
]
;
for
(
int i
=
0
; i
<
pyArray.length; i
+
+
)
{
PyObject pyGasStation
=
pyArray
[
i
]
;
/
/
Extract attributes from the PyObject representing a GasStation
String name
=
pyGasStation.get
(
"
name
"
)
.
toString
(
)
;
String location
=
pyGasStation.get
(
"
location
"
)
.
toString
(
)
;
/
/
Extract prices and print them
double regularPrice
=
pyGasStation.get
(
"
regular
_
price"
)
.
toDouble
(
)
;
double midgradePrice
=
pyGasStation.get
(
"
midgrade
_
price"
)
.
toDouble
(
)
;
double premiumPrice
=
pyGasStation.get
(
"
premium
_
price"
)
.
toDouble
(
)
;
/
/
System
.
out.println
(
"
Regular Price:
"
+
regularPrice
)
;
/
/
System
.
out.println
(
"
Midgrade Price:
"
+
midgradePrice
)
;
/
/
System
.
out.println
(
"
Premium Price:
"
+
premiumPrice
)
;
String rating
=
pyGasStation.get
(
"
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
1
,
station
2
)
-
>
Double.compare
(
station
1
.
getRegularPrice
(
)
,
station
2
.
getRegularPrice
(
)
)
)
;
return gasStations;
}
} Make 3 functions that take in the gas station class array as a variable
1 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions