正如您在我的CountriesTest课程中所看到的,我正在跟踪最大的人口,面积和密度。但是我想不出如何引用这些元素所处的位置,并使用Countries类中的相应方法来插入具有所述类别中最高值的国家的相应名称。我知道界面似乎对情况没用,但是任务需要它。我在代码中包含了笔记,我认为我遗漏的东西会消失。
编辑:这是作业的参数,因为我的问题有点令人困惑:
编写一个打印的程序:面积最大的国家,人口最多的国家和人口密度最大的国家。仅使用抽象类和抽象方法创建解决方案。
这是我的界面
public interface CountriesInterface
{
public String largestPop();
public String largestArea();
public String popDensity();
}
以下是访问该界面的类
public class Countries implements CountriesInterface
{
private String cName;
private int cPop;
private int cArea;
private int popDensity;
public Countries(String cName, int cPop, int cArea, int popDensity)
{
this.cName = cName;
this.cPop = cPop;
this.cArea = cArea;
this.popDensity = popDensity;
}
public String largestPop()
{
return cName;
}
public String largestArea()
{
return cName;
}
public String popDensity()
{
return cName;
}
}
这是我的测试类
import java.util.*;
public class CountriesTest
{
public static void main(String[] args)
{
int population = 0;
int area = 0;
int density = 0;
int arbitraryValue = 0;
int arbitraryValue2 = 0;
int arbitraryValue3 = 0;
String outputPop;
String outputArea;
String outputDensity;
ArrayList<String> countryList = new ArrayList<String>();
ArrayList<Integer> countryPopulation = new ArrayList<Integer>();
ArrayList<Integer> countryArea = new ArrayList<Integer>();
ArrayList<Integer> countryPopulationDensity = new ArrayList<Integer>();
Scanner myScanner = new Scanner(System.in);
boolean done = false;
do
{
System.out.println("1. Enter a country \n2. Print countries with the largest population, area, and population density \n3. Exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter name of country: ");
String input1 = myScanner.nextLine();
countryList.add(input1);
System.out.print("Enter area of country in square kilometers: ");
String input2 = myScanner.nextLine();
population = Integer.parseInt(input2);
countryPopulation.add(population);
System.out.print("Enter population of country: ");
String input3 = myScanner.nextLine();
area = Integer.parseInt(input3);
countryArea.add(area);
density = population/area;
countryPopulationDensity.add(density);
Countries aCountries = new Countries(input1, population, area, density);
if(population > arbitraryValue)
{
population = arbitraryValue;
//I don't know whats supposed to go here
}
if(area > arbitraryValue2)
{
area = arbitraryValue2;
//I don't know whats supposed to go here
}
if(density > arbitraryValue3)
{
density = arbitraryValue3;
//I don't know whats supposed to go here
}
}
else if(choice == 2)
{
System.out.println("The country with the largest population: " + I don't know whats supposed to go here);
System.out.println("The country with the largest area: " + I don't know whats supposed to go here);
System.out.println("The country with the largest population density is: " + I don't know whats supposed to go here);
}
else if(choice == 3)
{
done = true;
}
else
System.out.println("Invalid Choice");
}
while (!done);
System.exit(0);
}
}
答案 0 :(得分:2)
我担心您可能希望重新开始,因为您的代码从一开始就看起来不合适。每个Country应该由接口方法定义,这些接口方法返回名称String,填充int或long,int或long的区域以及密度,这可能是double。每个国家/地区都不需要最大任何内容,并且不应该返回largestXXX()
的方法,因为它应该是国家/地区集合的属性,而不是单个国家/地区。
为简化起见,您可能需要以下内容:
interface Country {
String getName();
int getPopulation();
// ... etc
}
class DefaultCountry implements Country {
// implement your methods
}
interface CountryCollection {
addCountry(Country country);
removeCountry(Country country);
Country getLargestPopulation();
Country getLargestArea();
....
}
好的,您的问题是实现CountryCollection接口的方法。具体类可能会保留List<Country>
并将其指定为ArrayList<Country>
。然后在getLargestXXX()
方法中,它使用for循环,遍历ArrayList,找到具有最大XXX的Country并返回该Country。例如,如果您正在尝试查找最大的总体,请在方法中创建一个int和Country局部变量,比如称为maximumPop和selectedCountry,并在每个Country上的循环调用getPopulation()
内。如果总体大于当前的最大流量,则将当前国家/地区的人口设置为maximumPop变量,将当前国家/地区设置为selectedCountry。在方法结束时,返回选定的国家/地区。