首页 > > 详细

辅导program、辅导Python,Java编程

File Input lab
The dataset in the info.txt file contains information from the World Happiness Report in 2022 collected by
Gallup.
Below are descriptions of each value. Each line represents data for one country. Each value in the line is
separated by a tab. For each line, the values are in the following order:
1. Country Name
2. Regional Indicators: CentralEastEurope, Asia, LatinAmerica, MiddleEastNorthAfrica, NorthAmericaANZ,
Africa, WesternEurope
3. GDP per capita is in terms of Purchasing Power Parity (PPP) adjusted to a constant 2017 international
dollars, taken from the World Development Indicators (WDI) by the World Bank (version 17, metadata last
updated on January 22, 2023). See Statistical Appendix 1 for more details. GDP data for 2022 are not yet
available, so we extend the GDP time series from 2021 to 2022 using country-specific forecasts of real GDP
growth from the OECD Economic Outlook No. 112 (November 2022) or, if missing, from the World Bank’s
Global Economic Prospects (last updated: January 10, 2023), after adjustment for population growth.
4. Freedom to make life choices (0-1) is the national average of binary responses to the GWP question "Are
you satisfied or dissatisfied with your freedom to choose what you do with your life?"
5. Happiness score (0-10) national average response to the question "Please imagine a ladder, with steps
numbered from 0 at the bottom to 10 at the top. The top of the ladder represents the best possible life for
you and the bottom of the ladder represents the worst possible life for you. On which step of the ladder
would you say you personally feel you stand at this time?"
Exceptional values
Data for some countries have not been included in the dataset. These values are "NA" instead of a numeric
value. The count of the number of countries in a region should not be affected by “NA”. The total number of
countries should be the same in the calculator of average GDP, average happiness score, and average
freedom score.
Converting a digit String into a double
You may need to use the Double.parseDouble method from the Wrapper class to convert a String into
a Double object. Call the method with a String argument and it will return a Double. The String
argument may only contain digit, ‘+’, or ‘-’ characters.
Using constants
When your data is divided up by categories, you can make the logic easier for your reader to understand by
introducing constants. Constants are variables that cannot change value during a program. In Java, a variable
is declared as a constant with the keyword final. By convention, we give constants a name all in capital
letters. For example, if your program contains the line:
final int AFRICA=0;
it can use AFRICA as the index number for all African data (in that row or column).
switch statements
Instead of using if-else if- else blocks, you can use a switch statement. The code is often easier to
write and read. See the syntax and example below. A switch statement can contain as many cases
needed.
switch(switch expression that holds a primitive value or String){
case value1(constant variable OR value with type that matches switch expression):
code;
//as many statements as need
break; (if no return statement)
case value2(constant variable OR value with type that matches switch expression):
code;
//as many statements as you need
break; (if no return statement)

default: (optional case that runs if no other case runs)
}
printf formatting
The printf method (from the same class as print and println) can be used to print formatted text in
many ways. To format text so that text is padded with extra spaces, you can set the width of the field. The
width represents the total number of characters printed. If the String contains fewer characters than the size of
the field’s width, spaces will be added before or after the String.
For example:
System.out.printf("%10s", "Test");
will print six spaces and then Test: Test
4
To print a String and then pad with spaces after, you include - before the size of the width.
For example:
System.out.printf("%-10s", "Test");
System.out.print("done");
will print Test and then six spaces:Test done
To print many different strings with different paddings, you can use many %width
For example:
System.out.printf("%-5s%-5s%5s%5s","a","b","c","d");
will print a with 4 padded spaces after, then b with 4 padded spaces after, then 4 padded spaces with c after,
then 4 padded spaces with d after:a b c d
To print a floating point value with a certain number of decimal places with a given width, you can also use the
printf method. The method will round to eliminate decimal places.
For example to print with 2 decimal places after:
System.out.printf("%10.2f", 1234.5678);
will print three spaces and then 1234.57: 1234.57
To print a floating point value with a certain number of decimal places with a given width and padding after use
the - character as before.
For example:
System.out.printf("%-10.3f", 1234.5678);
System.out.print("done");
will print 1234.568 and then two spaces:1234.568 done
Closing a Scanner instance
At the end of a method, you can close the stream on a Scanner object with the close instance method.
Often, this is optional; not closing the Scanner will not affect your program.
Assignment
To read the data in the file, you will need to set the line delimiter to a tab. In order to do so, call the
useDelimiter method with the appropriate Scanner instance. The argument will be the String that
represents a tab. Any method that uses a Scanner to read file input or calls a method that does so must
contain the code throws Exception in the header!
The class contains several variable declarations. dataTable is a 2D array of doubles that will hold the
average values for the data in each region. The 2D array should have 7 rows and 3 columns. Each row
represents one region. The first column should hold the average GDP, the second column should hold the
average Freedom Score, and the last column should hold the average Happiness Score. Class constants
have also been declared for each region. Consider using them in your program! Finally, a variable will store
the happiness score for the United States.
You must implement 5 methods. You may choose to write any private helper methods you want in addition to
these:
1) constructor: this method should initialize all instance variables and by the time the method is finished
executing, dataTable should contain all necessary averages
2) printData(): this method will print the formatted values stored in dataTable for the three 3 data
types for each region as shown below. The GDP should be formatted to 2 decimal places and start
with the ‘$’ symbole. The Freedom Score and Happiness Score should be formatted to 3 decimal
places. This method should not return a value.
The header should be the text labels each printed in a field of 20 characters (width of 20, with padding after).
You will need to figure out the code for this!
The label at the beginning of each row should be the text in a field of 20 characters (width of 20, with padding
after).
System.out.printf("%-20s",text label goes here);
The GDP value should be a floating point number with 2 decimal places in a field of 20 characters (width of 20,
with padding after). You can use:
System.out.printf("%-20.2f", floating value goes here)
The freedom score and happiness scores should be a floating point number with 3 decimal places in a field of
20 characters (width of 20, with padding after).
System.out.printf("%-20.3f", floating value goes here)
The first few lines that should be printed have been filled in for you. The total width of the field for the GDP
column will have a width of 21 characters (a width of 20 characters for the digits and one additional for the ‘$’
symbol).
World Region GDP Freedom Score Happiness Score
Africa $4552.58 0.707 4.549
Asia $13510.99 0.809 5.185
East Europe
Latin Amer
Middle East
N. Amer/ANZ
West Europe
3) poorButHappy(): returns an ArrayList of String country names (in alphabetic order) for which
the GDP is below the region’s average AND the happiness score is above the region’s average. The
country names are the names in the info.txt file.
4) freedomDifference(): returns a String that states the difference between the regions with the
highest and lowest average freedom scores (the scores in the data table, not the printed values). The
regional names should match those in the info.txt file. For example, if the maximum freedom score was
0.982 for WesternEurope and the lowest score was 0.651 for CentralEastEurope the returned String
should be "WesternEurope had the maximum freedom score and CentralEastEurope had the minimum
freedom score. The difference was 0.331." If two regions have the same minimum or maximum score,
store the first minimum score or first maximum score.
5) happierThanUs(): returns an ArrayList of String country names (in alphabetic order) for
which the Happiness Score is above the happiness score for the United States. The country names
are the names in the info.txt file.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!