Wednesday, March 26, 2014

RainfallApp v2

This is the RainFallApp v2. It take takes the input name of the two months, then finds the rainfall amount for that month and then outputs the average between the two months.


import java.util.Scanner;

public class RainFallApp {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the first month: ");
        String aMonth = input.nextLine();

        System.out.print("Please enter the second month: ");
        String bMonth = input.nextLine();


        rainfall aRainfall = new rainfall();
        String aName = aRainfall.rainAmount(aMonth);
        Double aAmount = Double.parseDouble(aName);

        rainfall bRainfall = new rainfall();
        String bName = bRainfall.rainAmount(bMonth);
        Double bAmount = Double.parseDouble(bName);

        double Avg = (aAmount + bAmount) / 2;

        System.out.println("\nIn the month of " + aMonth + " it had " + aAmount + " inches of rain.");
        System.out.println("In the month of " + bMonth + " it had " + bAmount + " inches of rain.");
        System.out.println("The average rainfall between the two months is: " + Avg);

    }

}

class rainfall {
    private String Rain_Amount;

    String rainAmount(String rainMonth) {
        Rain_Amount = getAmount(rainMonth);
        return Rain_Amount;
    }

    private String getAmount(String rainMonth) {

        if (rainMonth.equals("Jan")) {
            Rain_Amount = "3.3";

        }
        else if (rainMonth.equals("Feb")) {
            Rain_Amount = "2.3";
        }
        else {
            System.out.println("Not a valid month name");
        }
        return Rain_Amount;
    }
}


Friday, March 7, 2014

timeApp

**POSTPONED SINCE I NEED MORE INFO**


CMPS 1044 Program 3 Spring 2014 –  Long-Distance Calls


Problem solving steps & Manually Calculated Answers

Problem Description
A long-distance carrier charges the following rates for telephone calls between the U.S. and Mexico:

Starting time of Call (hour:minutes)
Rate Per Minute
00:00 06:59 (midnight 6:59 a.m.)
$ 0.12
07:00 19:00 (7 a.m. – 7 p.m.)
0.55
19:01 23:59 (after 7 p.m. before midnight)
0.35

Given the StartTime of a call and the Length of the call in minutes, the cost for the call is to be calculated.  The time is input as a decimal number. That is, 5:45 will be input as 5.45.  You MUST split this into the integer Hour and Minute components then validate the correct format of each one. When printing, you must print the time as 05:45, with the colon not the period.  Use a while loop counting to 17, like you did in the first 2 programs. ** Use if statements, properly nested, to solve the problem.  All I/O is to be file based.

Data File ( Name, StartTime, Length (in minutes))
Mary
5.45
10
Joe
16.41
40
Ted
23.10
38
Joshua
45.9
2
Fred
12.42
50
Caroline
23.00
121
Jeffrey
19.00
82
Joy
24.00
13
Paul
06.59
8
Tony
7.00
29
Henry
19.01
59
Catherine
23.08
134
Titus
10.60
65
Julio
5.37
100
Rebekah
12.03
81
Andy
23.59
5
TalkMonkey
22.00
300

Input Validation:  You must check Hour & Minute after reading it in.  That is, if Hour is greater than 23 or if Minute is greater than 59, then you will print the word ERROR in the CHARGE column. Otherwise, you calculate and print the total cost.

Output Form:  Make sure that your output looks as below.
Your Name

Long Distance Telephone Bills
Name   Start Time Length    Charge
-------------------------------------------------------- David     12:42    100      $ 55.00   NOTE: Alignment
Julie     06:05     10      $  1.20   NOTE: Must manually print 0
Max       25:50     12        ERROR        for single digit times.
Cindy     12:89    100        ERROR


Thursday, March 6, 2014

expApp

Programming Fundamentals
CS1336

Assignment #4 – Calculations and Selection Logic

Introduction
Your second programming assignment will consist of two small C++ programs. Each program will be independent of the other program. Each one should compile correctly and produce the specified output.
Please note that each of the programs should comply with the commenting and formatting rules we discussed in class. For example, there should be a header for the whole program that gives the author’s name, class name, date, and description. End braces should be commented, and there are alignment and indenting requirements as discussed. Please ask if you have any questions.

Program #1
For a more complex calculation program, please implement Problem 17 on page 146 of our text.
You may use the pow() function in the cmath header. This function has the following prototype:
double pow (double base, double power);
The “base” is the number that is being taken to some power, and the “power” is the power to which the base is taken. The function will return a double as the result.

Here is a scan from the book to help some of you who may not have the text.

package expApp;
import java.text.*;
import java.util.Scanner;

public class expApp {

 public static void main(String[] args) {
    
  Scanner input = new Scanner(System.in);
  DecimalFormat df = new DecimalFormat("0.00");

  double L;
  double interest;
  int N;
  
  System.out.print("Please enter your loan amount: ");
  L = input.nextDouble();
  
  System.out.print("Please enter your interest rate for the loan: ");
  interest = input.nextDouble();
  
  System.out.print("Lastly how many payments will you be making: ");
  N = input.nextInt();
  
  double Rate = ((interest / 12));
  double dRate = Rate / 100;
  double Payments = ((dRate * Math.pow((1+dRate), N)) / ((Math.pow((1+dRate), N)) -1)) *L;
  double totalPaid = (Payments * N);
  double interestPaid = totalPaid - L;
  
  System.out.println("\nLoan Amount:           $  " + df.format(L));
  System.out.println("Monthly interest rate:    " + Math.round(Rate) +"%");
  System.out.println("Number of Payments:       " + N);
  System.out.println("Monthly Payments of:   $  " + df.format(Payments));
  System.out.println("Amount Paid Back:      $  " + df.format(totalPaid));
  System.out.println("Interest Paid:         $  " + df.format(interestPaid));
 }

}

Wednesday, February 19, 2014

testApp

Part 3: Grading History Program

Problem description:

Your history instructor gives three tests worth up to 50 points each. Even though there are three tests, only two of them will be kept. Write a program that calculates and displays the letter grade received in the course using the grading scale shown below after the user enters the test scores. You need to determine which of tests 1 and 2 is being dropped, you need to keep track of which test was dropped, you need to calculate the total points for the two test scores used and you need to determine the letter grade earned. Note that you are not required to validate the test values. The output should list all three test scores, state which of the first two tests was dropped (test 1 or test 2), and give the total points for the two tests used, and letter grade earned.

Use the following table for assigning the letter grade.

Points                   Letter Grade
>= 92                   A
< 92, >= 82          B
< 82, >= 72          C
< 72                    F

For example, given below is a sample run of a program that satisfies the requirements of this exercise.

Enter the score for test #1: 45[Enter]
Enter the score for test #2: 15[Enter]
Enter the score for test #3: 37[Enter]
First test: 45
Second test: 15
Third test: 37
After dropping test #2, the points earned are 82.
The letter grade is B.

Test your program giving the same inputs in the sample run above.

package testApp;

package testApp;

import java.util.Scanner;

public class testApp {

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  int testone, testtwo, testthree;
  int total;

  System.out.print("Enter the score for test #1: ");
  testone = input.nextInt();
  System.out.print("Enter the score for test #2: ");
  testtwo = input.nextInt();
  System.out.print("Enter the score for test #3: ");
  testthree = input.nextInt();

  System.out.println("First test: " + testone);
  System.out.println("Second test: " + testtwo);
  System.out.println("Third test: " + testthree);

  if (testone > testtwo) {
   total = testone + testthree;
   System.out.println("After dropping test #2, the points earned are "
     + total);

  } else {
   total = testtwo + testthree;
   System.out.println("After dropping test #1, the points earned are "
     + total);

  }

  if (total >= 92) {
   System.out.println("The letter grade is A");
  } else if (total >= 82) {
   System.out.println("The letter grade is B");
  } else if (total >= 72) {
   System.out.println("The letter grade is C");
  } else {
   System.out.println("The letter grade is F");
  }
 }
}

Wednesday, February 12, 2014

conversionApp

CMPS 1043 - Program #1

Measurement Conversion

Write a C++ program to convert a measurement given in feet to the equivalent number of (a)
yards, (b) inches, (c) centimeters, and (d) meters. Input the number of feet from the keyboard and print the equivalent measures into a file as shown below.

1 ft. = 12 in.     1 yd. = 3 ft.     1 in. = 2.54 cm.     1 m. = 100 cm.

OUTPUT

2.00000 feet equals //this section will be repeated for each of the 10 data items
0.6667 yards
24 inches
60.96 centimeters
0.6096 meters

Use type double for all variables. Be sure to put in prompts asking for data.
Use the following main function to cause the program to execute 10 times for different data
values. Check your output, making sure your answers are correct. Comment your program as discussed in class.

int main (void)
{
          int count;
          ........PUT YOUR DECLARATIONS HERE.........
         count = 1;
         while (count <= 10)
                  {... PUT YOUR STATEMENTS HERE...
                  count = count + 1;
                   }
          return 0;
}

Use the following data to enter into the keyboard:
2         39.209   3            100.0    78.921
86.5    10          34912    45.88    87.09


package graceApp;
 
import java.text.*;
import java.util.Scanner;
 
public class graceApp {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  double feet;
  DecimalFormat df = new DecimalFormat("#.##");
  System.out.println("This program will run 5 times before it quits...");
  System.out.println("");
 
  for (int i = 1; i <= 10; i++) {
   System.out.print("Please enter amount of feet: ");
   feet = input.nextDouble();
 
   double yards = feet / 3;
   double inches = feet * 12;
   double centimeters = feet * 30.48;
   double meters = feet / 30.48;
 
   System.out.println(df.format(yards)+ "yd  "+ (df.format(inches) + "in  " + (df.format(centimeters)+ "cm  " + (df.format(meters) + "m  "))));
   System.out.println("");
 
  }
 }
}

rainApp


Programming Fundamentals
CS1336

Assignment #3 - Basic Calculations Continued

Program #2 - Using a string variable

For the second problem, please implement Problem 4 on page 143 of the text. A scan of the problem is provided below.

This problem asks you to calculate the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should then display a message similar to the following:

The average rainfall for June, July, and August was 6.72 inches.

Note that the rainfall average is printed to two decimal places this time.

What is different in this problem is that we are asking the user for string data, and not just numeric input. To accomplish that in a C++ program, we can use the string class. Simply declare three variables of type string to receive the month name input from cin. Don’t forget to include the <string> header file.


package rainApp;
 
import java.util.Scanner;
import java.text.*;
 
public class rainApp {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  DecimalFormat df = new DecimalFormat("0.00");
  double rainfall, avgrainfall;
  double totalrainfall = 0;
 
  System.out.println("This program will find 3 months average rainfall");
 
  System.out.print("Type three months and press enter after each one \nMonth 1: ");
  String monthone = input.nextLine();
  System.out.print("Month 2: ");
  String monthtwo = input.nextLine();
  System.out.print("Month 3: ");
  String monththree = input.nextLine();
 
  System.out.print("How many inches of rain fell for " + monthone + "? ");
  rainfall = input.nextDouble();
  totalrainfall += rainfall;
 
  System.out.print("How many inches of rain fell for " + monthtwo + "? ");
  rainfall = input.nextDouble();
  totalrainfall += rainfall;
 
  System.out.print("How many inches of rain fell for " + monththree+ "? ");
  rainfall = input.nextDouble();
  totalrainfall += rainfall;
 
  avgrainfall = (totalrainfall / 3);
  System.out.print("The average rainfall for months " + monthone + ", "+ monthtwo + ", and " + monththree + " is: "+ (df.format(avgrainfall) + "in"));
 
 }
 
}

Tuesday, February 11, 2014

avgApp

Programming Fundamentals
CS1336

Assignment #3 - Basic Calculations Continued

Program #1 – Calculating Averages

For this problem, please implement Problem 3 on page 143 of the text. A scan of the problem is provided below.

In this problem, the program should obtain five test scores from the user. As always, the program should print a prompt before it gets the user input from the keyboard. You can either print out five separate prompts or one prompt, but there should be enough information in the prompt so that it is clear to the user what he/she needs to do. Your program will then calculate the average of the five scores and print out the result.

Please print out the result to one decimal place. Your output can look like:

The average of these test scores is: 94.5.


import java.util.Scanner;
import java.text.*;
 
public class Assignment3 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  double test;
  DecimalFormat df = new DecimalFormat("#.#");
  System.out
    .println("In this program you will find the average of 5 test scores...");
  System.out.println("");
 
  double test1;
  double test2;
  double test3;
  double test4;
  double test5;
 
  System.out.print("Please enter test score 1: ");
  test1 = input.nextDouble();
 
  System.out.print("Please enter test score 2: ");
  test2 = input.nextDouble();
 
  System.out.print("Please enter test score 3: ");
  test3 = input.nextDouble();
 
  System.out.print("Please enter test score 4: ");
  test4 = input.nextDouble();
 
  System.out.print("Please enter test score 5: ");
  test5 = input.nextDouble();
 
  double average = (test1 + test2 + test3 + test4 + test5) / 5;
  System.out.println("Your average test score is: "+ (df.format(average)));
 
 }
 
}