Menu
support@authoritypapers.com
+1(805) 568 7317

hello chegg objective print one and two dimensional arrays in very specific table fo 5151783

Hello Chegg,

Objective

  • Print one and two dimensional arrays in very specific table format

Background reading

  • Chapter 10.4 Formatting output. Read this first to learn about format specifiers.

Assignment

Did you know there are many cities in the world with population over 10 million people? Furthermore, none of the top twenty most populous cities are in the United States. The populations are such large numbers that they don't fit in an int type. We need to choose between a long–which makes sense because we have whole persons–or a double, which might be useful when we find percent. Don't worry. I made the choice for you, but you might challenge might choice with good cause!

You will write a program that displays city name, country, population, and percent of the country's population that live in the city. An example output follows (cities in all CAPS represent capitols).

City Country Population Percent —————————————————————————————————————————————————————— Shanghai China 24,153,000 1.744 Seoul South Korea 10,290,000 20.259 Chengdu China 10,152,000 0.733 KINSHASA Congo D.R. 10,125,000 11.732 LIMA Peru 9,752,000 30.692 New York United States 8,543,543 43.036

Data Structures

The country names can be stored in a one-dimensional String array. A corresponding two-dimensional array will hold the population counts: the city population and the country total population.

Organizing the program into methods

Your program must have these public static methods. Order them in your code where it makes sense to you, just make main() the first or last one.

  • main(String[] args)

    • This method should consist only of calls to methods: getCities(); getPopulations(); printHeader(); printStats().
    • Two or four lines should be sufficient for the body of the method.
    • There are no loops in the main() method.
  • void printHeader()

    • outputs table column header: City, Country, Population, Percent separated by spaces (no tabs!)
    • “Percent” means what percentage of the total country population lives in this city

    You will be tempted to do this:
    System.out.println(” City Country Population Percent”);

    Let's not. Adjusting column width is much better in the long run if done using formatting methods. For each string, use %s as a placeholder for the string argument that follows. They will be substituted from left to right in the order given. To separate columns, add the width value and if you want it left-justified, add a minus sign.
    System.out.printf(“%-15s%-17s%15s%12sn”, “FirstStringArg”, “SecondStringArg”, “ThirdStringArg”, “FourthStringArg”);
    Now, that is not precisely the correct widths, but what fun would it be if you couldn't experiment?

  • String repeat(char ch, int numOfCharsInString)

    • Returns a string the length of numOfCharsInString consisting of only the character ch.
    • Instead of printing a dash (that is, the minus sign), let's print a Unicode character for the “em dash” which is a longer dash. Look up its value in a Unicode Table. It will show U+aNumber. For example, a space is U+0020. So get the number for the em dash and precede it with a backslash-u, for example, a space would be:
      char space = 'u0020';
  • void printStats(String[] cities, double[][] population)

    • Loops through the parallel arrays printing data and performing the division calculation to produce the Percent value.
    • This time use System.out.format which is identical to System.out.printf but we are trying to build our language skills.
    • To print floating-point values, we use %f (if you mistakenly try to use %d it will expect a decimal number, that is, a whole number like int, short, etc.).
    • Here's a nifty trick: instead of outputting 24153000 or writing a dastardly method to insert commas, the method can insert them for us (Yay!). For example, %,12.0fwill print a minimum width of 12, no trailing decimal places, and insert commas between triples.
  • String getIdentificationString()

    • Returns “Program 1c, Student Name”
  • double[][] getPopulations()

    • Creates and returns a two-dimensional array containing the following data:

24153000,1384688986 18590000,1384688986 18000000,207862518 14657000,81257239 14543000,162951560 13617000,126168156 13197596,143964513 12877000,105920222 12784000,1384688986 12400000,1296834042 12038000,207652865 11908000,1384688986 11548000,1384688986 11035000,1296834042 10608000,1384688986 10355000,207862518 10290000,50791919 10152000,1384688986 10125000,86300000 9752000,31773839

  • String[] getCities()
    • Creates and returns a one-dimensional String array containing the following data:

“Shanghai,China” “BEIJING,China” “Karachi,Pakistan” “Istanbul,Turkey” “DHAKA,Bangladesh” “TOKYO,Japan” “MOSCOW,Russia” “MANILA,Philippines” “Tianjin,China” “Mumbai,India” “Sao Paulo,Brazil” “Shenzhen,China” “Guangzhou,China” “DELHI,India” “Wuhan,China” “Lahore,Pakistan” “Seoul,South Korea” “Chengdu,China” “KINSHASA,Congo D.R.” “LIMA,Peru”

Notes:

  • The first city+country name corresponds to the first row in the population array, and so on.
  • Print only cities with population over 10 million. When traversing an array, use the array length property to ensure your program does not get an ArrayIndexOutOfBoundsException error.

Can someone help me with this code, Thanks!

______________________________________________________________

public class Populations { public static void main(String[] args) { String[] cities = getCitie(); double[][] population = getPopulations(); printHeader(); printStats(cities, population); } public static double[][] getPopulations() { double[][] population = { {24153000, 1384688986}, {18590000, 1384688986}, {18000000, 207862518}, {14657000, 81257239}, {14543000, 162951560}, {13617000, 126168156}, {13197596, 143964513}, {12877000, 105920222}, {12784000, 1384688986}, {12400000, 1296834042}, {12038000, 207652865}, {11908000, 1384688986}, {11548000, 1384688986}, {11035000, 1296834042}, {10608000, 1384688986}, {10355000, 207862518}, {10290000, 50791919}, {10152000, 1384688986}, {10125000, 86300000}, {9752000, 31773839} }; return (population); } public static String[] getCitie() { String[] City = new String[20]; City[0] = “Shanghai,China”; City[1] = “BEIJING,China”; City[2] = “Karachi,Pakistan”; City[3] = “Istanbul,Turkey”; City[4] = “DHAKA,Bangladesh”; City[5] = “TOKYO,Japan”; City[6] = “MOSCOW,Russia”; City[7] = “MANILA,Philippines”; City[8] = “Tianjin,China”; City[9] = “Mumbai,India”; City[10] = “Sao Paulo,Brazil”; City[11] = “Shenzhen,China”; City[12] = “Guangzhou,China”; City[13] = “DELHI,India”; City[14] = “Wuhan,China”; City[15] = “Lahore,Pakistan”; City[16] = “Seoul,South Korea”; City[17] = “Chengdu,China”; City[18] = “KINSHASA,Congo D.R.”; City[19] = “LIMA,Peru”; return (City); } public static void printHeader() { System.out.println(” City Country Population Percent”); System.out.printf(“%-15s%-17s%15s%12sn”, “FirstStringArg”, “SecondStringArg”, “ThirdStringArg”, “FourthStringArg”); char space = 'u0020'; } public static String repeat(char ch, int numOfCharsInString) { numOfCharsInString = ch; return; } public static void printStats(String[] cities, double[][] population) { System.out.format(); } public static String getIdentificationString () { return “Program 1c, Student Name”; } }

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now