cover picture cover picture


Homework 2

CMP 167: Programming Methods I
Lehman College, City University of New York
Fall 2016


This Homework Is Due By 11:59 PM on Tuesday October 4, 2016

Textbook Section 2.22 Homework 2-1

Write a program that asks the user for three integers (firstInt, secondInt, thirdInt), perform the following operations and output the results:

  1. Add firstInt and secondInt then divide the result by thirdInt
  2. Multiply secondInt by thirdInt and divide result by the sum of secondInt and firstInt
  3. Multiply firstInt by thirdInt and find the remainder of dividing the result by secondInt

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

    Enter firstInt: 
    Enter secondtInt: 
    Enter thirdInt: 
  

Your output must be of the format:

    First result = firstResult 
    Second result = secondResult 
    Third result = thirdResult 
  

Please note that your class should be named IntegerExpressions.

Textbook Section 2.23 Homework 2-2

Write a program that asks the user for time in days and prints the number of years and days (assume that there are 365 days in a year). Note that you should use "years" and "days" for all outputs (even in the cases where it is grammatically incorrect).

For example:

    If the user entered: 367, your program would output: 1 years and 2 days.
    If the user entered: 750, your program would output: 2 years and 20 days
    If the user entered, 1000, your program would output, 2 years and 270 days.
  

Your prompt to the user to enter the number of days must be:

    Enter number of days:
  

Your output must be of the format:

    numYears years and numDays days. 
  

Please note that your class should be named YearsAndDays.

Textbook Section 2.24 Homework 2-3

Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate:

    Circumference of a circle with that radius (circleCircumference = 2π r)
    Area of a circle with that radius (circleArea = π r2)
    Area of a sphere with that radius (sphereArea = 4π r2)
    Volume of a sphere with that radius (sphereVolume = 43π r3)
    
    You must use π as defined in the java.lang.Math class.
  

Your prompt to the user to enter the number of days must be:

    Enter radius:
  

Your output must be of the format:

    Circle Circumference = circleCircumference
    Circle Area = circleArea
    Sphere Area = sphereArea
    Sphere Volume = sphereVolume
  

Please note that your class should be named CircleSphere.

Textbook Section 2.25 Homework 2-4

In a cartisian coordinate plane, the distance between two point P1 with coordinates (X1, Y1) and point P2 with coordinates (X2, Y2) is given by the formula


dist = √   (X2 - X1) 2 (Y2 - Y1) 2  

Write a program that prompts the user for the coordinates of both points, please note that all coordinates are double values. Your prompts must be:

    Coordinates for P1
    Enter X1:
    Enter Y1:
    Coordinates for P2
    Enter X2:
    Enter Y2:
  

Your output must be of the format:

    Distance between P1 and P2 = computed distance
  

Please note that your class should be named DistanceFormula.

Please submit the completed assignment in the corresponding section(s) in your textbook Lehman College, City University of New York CMP 167 Fakhouri Fall 2016: Programming in Java with zyLabs . No other forms of submission will be accepted.