You are on page 1of 6

Homework 2

CMP 167: Programming Methods I


Lehman College, City University of New York

This homework assignment must be completed in your textbook:

ZYBooks -- CMP-167: Programming Methods I by following the link for each problem.

No other forms of submission will be accepted.

No late submissions will be accepted.

Problem definitions for this homework assignment are found on the pages that follow below.

A note on formatting in the problem definitions:

Text that is in boldface represents values input by the user.

Text that is underlined represents values that are calculated by the program.

Homework 2-1

Write a program that asks the user for three integers.


The program must then output results based on the following calculations:

firstResult = The sum of firstInt and secondInt divided by thirdInt

secondResult = The product of secondInt and thirdInt divided by the sum of secondInt and firstInt

thirdResult = The product of firstInt and thirdInt mod by the secondInt


Note:

firstInt, secondInt, and thirdInt represent the three integers entered by the user.

firstResult, secondResult, and thirdResult represent the results of the calculations.

n mod m means the remainder obtained when n is divided by m.

The prompt to the user to enter the integers must be:

Enter firstInt:
Enter secondInt:
Enter thirdInt:

The output must be in the format:

First Result = firstResult


Second Result = secondResult
Third Result = thirdResult

Please make sure to end each line of output with a newline.

Please note that your class should be named IntegerExpressions.

Homework 2-2

Write a program that asks the user to enter a distance measured in inches.
The program must then determine and output the equivalent number of yards, feet, and inches.

Note:

3 feet = 1 yard

12 inches = 1 foot

Use plurals "yards" , “feet”, and "inches" for all outputs (even where it is grammatically incorrect).

The prompt to the user to enter the number of inches must be:

Enter number of inches:

The output must be in the format:

numYards yards, numFeet feet, and numInches inches.


For a sample run where the user enters 367 the console screen must look as follows:

Enter number of inches: 367


10 yards, 0 feet, and 7 inches.

Please make sure to end each line of output with a newline.

Please note that your java class should be named YardsFeetInches.

Homework 2-3

Write a program that prompts the user for a double value representing a radius.
The program must then use the radius to determine and output the following:

circleCircumference = 2πr

circleArea = πr2

sphereArea = 4πr2

sphereVolume = (4/3)πr3

Note:

You must use π as defined in the java.lang.Math class.

Recall that int, float,and double divisions produce different results.

Recall that there are multiple ways to calculate r2 and r3.

Recall that floating point arithmetic rounds differently for different formulations of a calculation.

The prompt to the user to enter the radius must be:

Enter radius:

The output must be in the format:

Circle Circumference = circleCircumference


Circle Area = circleArea
Sphere Area = sphereArea
Sphere Volume = sphereVolume
Please make sure to end each line of output with a newline.

Please note that your class should be named CircleSphere.

Homework 2-4

Write a program that prompts the user for the coordinates of two points in a Cartesian Plane.
The program must then compute and output the distance between those points.

Note:

All coordinates need to be double values.

In a cartesian plane the distance between two points, P1 = (x1, y1) and P2 = (x2, y2), is given by the formula

( x2 - x 1 ) 2 + ( y 2 - y 1 ) 2
dist = √  
The prompt to the user must be:

Coordinates for P1
Enter x1:
Enter y1:
Coordinates for P2
Enter x2:
Enter y2:

The output must be in the format:

Distance between P1 and P2 = dist

Please make sure to end each line of output with a newline.

Please note that your class should be named DistanceFormula.

Homework 2-5

Write a Java program that asks the user to enter four words.
The program should then analyze each of the four words so that it can report the length of each word as well
as the positions of the five vowels within each word.

For a sample run where the user enters the words: she smiles third frisbee
the console screen must look as follows:

Enter 4 words:
she smiles third frisbee
Word 1 = "she" Length = 3 position of vowels: 'a' = -1, 'e' = 2, 'i' = -1, 'o' = -1, 'u' = -1
Word 2 = "smiles" Length = 6 position of vowels: 'a' = -1, 'e' = 4, 'i' = 2, 'o' = -1, 'u' = -1
Word 3 = "third" Length = 5 position of vowels: 'a' = -1, 'e' = -1, 'i' = 2, 'o' = -1, 'u' = -1
Word 4 = "frisbee" Length = 7 position of vowels: 'a' = -1, 'e' = 5, 'i' = 2, 'o' = -1, 'u' = -1

For a sample run where the user enters the words: shoe shoe chime wallabees
the console screen must look as follows:
Enter 4 words:
shoe shoe chime wallabees
Word 1 = "shoe" Length = 4 position of vowels: 'a' = -1, 'e' = 3, 'i' = -1, 'o' = 2, 'u' = -1
Word 2 = "shoe" Length = 4 position of vowels: 'a' = -1, 'e' = 3, 'i' = -1, 'o' = 2, 'u' = -1
Word 3 = "chime" Length = 5 position of vowels: 'a' = -1, 'e' = 4, 'i' = 2, 'o' = -1, 'u' = -1
Word 4 = "wallabees" Length = 9 position of vowels: 'a' = 1, 'e' = 6, 'i' = -1, 'o' = -1, 'u' = -1

Note:

For multiple instances of a vowel, only the index of the first occurrence will be indicated.

An index of -1 indicates that the vowel does not occur in the word.

Please make sure to end each line of output with a newline.

Please note that your class should be named Words.

Homework 2-6

You just returned from a trip. The countries you visited were Jamaica, The Dominican Republic, and Cuba.
You arrived back in the United States with some foreign currency from these countries.

Write a Java program that prompts the user for an amount of each of these foreign currencies.
The program should convert these combined amounts to the total equivalent number of US Dollars.
The program should then output the result.

Use exchange rates for these currencies as follows:

1 Jamaican Dollar = 0.0069 US $


1 Dominican Peso = 0.017 US $
1 Cuban Peso = 0.042 US $

The prompts should look like this:

Enter number of Jamaican Dollars :


Enter number of Dominican Pesos :
Enter number of Cuban Pesos :

The output should be formatted as follows:

US Dollars = $total amount of US Dollars

Hint:

Be very careful with formatting your output. The number should look like it represents money.

System.out.printf( "US Dollars = $%.2f\n" , totalUnitedStatesDollars );

You might also like