SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Console Input And Output
Exercise:
Programming Projects
Question:13 | ISBN:9780132830317 | Edition: 5

Question

(This is an extension of an exercise from Chapter 1.) A simple rule to estimate your ideal body weight is to allow 110 pounds for the first 5 feet of height and 5 pounds for each additional inch. Create the following text in a text file. It contains the names and heights in feet and inches of Tom Atto (6’3”), Eaton Wright (5’5”), and Cary Oki (5’11”):

Tom Atto

6

3

Eaton Wright

5

5

Cary Oki

5

11

Write a program that reads the data in the file and outputs the full name and ideal body weight for each person. In the next chapter, you will learn about loops, which allow for a more efficient way to solve this problem.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// IdealBodyWeight.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class IdealBodyWeight
{
	public static int WEIGHT_FOR_MINIMUM_HEIGHT_IN_POUNDS = 110;
	public static int MINIMUM_ALLOW_HEIGHT_IN_FEET = 5;
	public static int WEIGHT_PER_ADDITIONAL_INCH_IN_POUNDS = 5;
	public static int INCHES_PER_FEET = 12;
	
	public static void main(String[] args) 
	{		
		Scanner infile = null ;
		
		try 
		{
			infile = new Scanner(new FileInputStream("data.txt"));
		} 
		catch (FileNotFoundException e) 
		{
			System.out.println("File not found.");			
			System.exit(0);
		}
		
		String personName;
		int personHeightInFeet;
		int personHeightInInches;
		double idealBodyWeightInPounds;
		
		System.out.println("Text left to read in file? " 
							+ infile.hasNextLine());
		personName = infile.nextLine();
		personHeightInFeet = infile.nextInt();
		infile.nextLine();
		personHeightInInches = infile.nextInt();
		infile.nextLine();
		
		idealBodyWeightInPounds = WEIGHT_FOR_MINIMUM_HEIGHT_IN_POUNDS
				+ ((personHeightInFeet - MINIMUM_ALLOW_HEIGHT_IN_FEET)
						* INCHES_PER_FEET + personHeightInInches)
						* WEIGHT_PER_ADDITIONAL_INCH_IN_POUNDS;

		System.out.println("  Name: " + personName + ", Ideal Body Weight: "
						+ idealBodyWeightInPounds + " pounds");
		System.out.println();
		
		System.out.println("Text left to read in file? " 
						+ infile.hasNextLine());
		personName = infile.nextLine();
		personHeightInFeet = infile.nextInt();
		infile.nextLine();
		personHeightInInches = infile.nextInt();
		infile.nextLine();
		
		idealBodyWeightInPounds = WEIGHT_FOR_MINIMUM_HEIGHT_IN_POUNDS 
				+ ((personHeightInFeet - MINIMUM_ALLOW_HEIGHT_IN_FEET)
						* INCHES_PER_FEET + personHeightInInches)
						* WEIGHT_PER_ADDITIONAL_INCH_IN_POUNDS;

		System.out.println("  Name: " + personName + ", Ideal Body Weight: "
						+ idealBodyWeightInPounds + " pounds");
		System.out.println();
		
		System.out.println("Text left to read in file? " 
						+ infile.hasNextLine());
			
		
		personName = infile.nextLine();
		personHeightInFeet = infile.nextInt();
		infile.nextLine();
		personHeightInInches = infile.nextInt();
		infile.nextLine();
		
		idealBodyWeightInPounds = WEIGHT_FOR_MINIMUM_HEIGHT_IN_POUNDS
			+ ((personHeightInFeet - MINIMUM_ALLOW_HEIGHT_IN_FEET)
					* INCHES_PER_FEET + personHeightInInches)
					* WEIGHT_PER_ADDITIONAL_INCH_IN_POUNDS;

		System.out.println("  Name: " + personName + ", Ideal Body Weight: "
						+ idealBodyWeightInPounds + " pounds");
		System.out.println();
		
		System.out.println("Text left to read in file? " 
						+ infile.hasNextLine());
		
		infile.close();
	}
}

Input File: data.txt

Tom Atto
6
3
Eaton Wright
5
5
Cary Oki
5
11

Output:

Text left to read in file? true
  Name: Tom Atto, Ideal Body Weight: 185.0 pounds

Text left to read in file? true
  Name: Eaton Wright, Ideal Body Weight: 135.0 pounds

Text left to read in file? true
  Name: Cary Oki, Ideal Body Weight: 165.0 pounds

Text left to read in file? false

 

0 0

Discussions

Post the discussion to improve the above solution.