Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FiveCards Lab in Java. Help. Hello Chegg, I have been tyring to complete this lab for quite some time now but have seemed to get

FiveCards Lab in Java. Help.

Hello Chegg,

I have been tyring to complete this lab for quite some time now but have seemed to get stuck and was wodnering if I could get assistants to finsh the class. I also attached (the bottom) my own code, and was wondering if we could use it to finsh the rest of the lab.

Thank You so much! ( I always rate positively ) ..... :)

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).

image text in transcribed

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%12s ", "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.

-----[MY CODE, PLEASE USE THANKS!]---- public class Populations {

public static void printHeader() { System.out.printf("%-15s%-17s%5s%12s ", "City", "Country", "Population", "Percent"); } public String repeat(char ch, int numOfCharsInString) {

} public static void printStats(String[] cities, double[][] population) { for(int i=0; i

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

Recommended Textbook for

MFDBS 91 3rd Symposium On Mathematical Fundamentals Of Database And Knowledge Base Systems Rostock Germany May 6 9 1991

Authors: Bernhard Thalheim ,Janos Demetrovics ,Hans-Detlef Gerhardt

1991st Edition

3540540091, 978-3540540090

More Books

Students also viewed these Databases questions

Question

=+2. What are the organization's relationship goals on this issue?

Answered: 1 week ago

Question

Language in Context?

Answered: 1 week ago