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:
Getting Started
Exercise:
Programming Projects
Question:8 | ISBN:9780132830317 | Edition: 5

Question

The following program will compile and run, but it uses poor programming style. Modify the program so that it uses the spelling conventions, constant naming conventions, and formatting style recommended in this book.

public class messy {

public static void main(String[] args)

{

double TIME; double PACE;

System.out.println("This program calculates your pace given a time

and distance traveled.");

TIME = 35.5; /* 35 minutes and 30 seconds */

PACE = TIME / distance;

System.out.println("Your pace is " + PACE + " miles per hour.");

}

public static final double distance = 6.21;

}


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// Messy.java
public class Messy
{
	public static final double DISTANCE = 6.21;

	public static void main(String[] args)
	{
		double time;
		double pace;

		System.out.println(
				"This program calculates your pace 
                                      given a time and distance traveled.");
		
		time = 35.50; /* 35 minutes and 30 seconds */
		
		pace = time / DISTANCE;
		
		System.out.printf("Your pace is %.1f minutes per mile.", pace);
	}
}

Output:

This program calculates your pace given a time and distance traveled.
Your pace is 5.7 minutes per mile.

 

0 0

Discussions

Post the discussion to improve the above solution.