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)));
 
 }
 
}

tempApp

Programming Fundamentals
CS1336

Assignment #2 - Basic Calculations

Program #2

Your second program is a modification of Problem 10 on page 144 of our text. In that problem,
the user inputs a temperature value in degrees Celsius and the program then calculates the
equivalent temperature in Fahrenheit.

Let’s modify the problem in the following way. Rather than requiring the user to input just a
Celsius temperature and then calculating the Fahrenheit temperature from it, let’s calculate
equivalent temperatures in both directions. In addition, we’ll add to the report the equivalent
temperate in a third temperature scale, namely, degrees Kelvin.

We’ll first ask the user for a Celsius temperature and then print a report about both the
Fahrenheit and the Kelvin temperatures. The output should like the following:

Please enter a number in degrees Celsius: 45.2

The equivalent Fahrenheit temperature is: 113.36 degrees.
The equivalent Kelvin temperature is: 318.36 degrees.

Your program will then ask the user to input a number in degrees Fahrenheit and print the
equivalent Celsius and Kelvin degrees:

Please enter a number in degrees Fahrenheit: 45.2

The equivalent Celsius temperature is: 7.33 degrees.
The equivalent Kelvin temperature is: 280.49 degrees.

Note that all calculated temperatures are printed to two decimal places.


import java.util.Scanner;
import java.text.DecimalFormat;

public class TempApp {

 public static void main(String[] args) {

  DecimalFormat df = new DecimalFormat("0.00");
  Scanner input = new Scanner(System.in);

  double celsius;

  System.out.print("Please enter a number in degrees Celsius: ");
  celsius = input.nextDouble();

  double fahrenheit = 1.8 * celsius + 32;
  System.out.println("The equivalent Fahrenheit temperature is: "
    + (df.format(fahrenheit)));
  double kelvin = celsius + 273.16;
  System.out.println("The equivalent Kelvin temperature is: " + (df.format(kelvin)));
 }

}

gasApp

Programming Fundamentals
CS1336

Assignment #2 - Basic Calculations

Program #1

In that problem is described a car that can go 21.5 miles per gallon when driven in town and 26.8 miles per gallon when driven on the highway. The problem in the book then asks the programmer to calculate how far that car can go on one tank of gas under both conditions.

Let’s modify that problem in the following way. We’ll assume that the user of the program is going to take a trip, and that part of the trip will be on the highway and part of it will be in town. Have your program ask the user how far he has to go under each condition. Your program will then print out the total number of gallons required for the trip.

For example, let’s say the user plans to take a trip that will require 120 miles of in-town driving and 250 miles of highway driving (we’re not trying to be realistic :-)). Your program run might look like the following:

Please enter the number of in-town driving miles: 120
Please enter the number of highway driving miles: 250

The total number of gallons required is: 14.9 gal.


import java.util.Scanner;

public class GasApplicatoin {

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

  double townmiles, highwaymiles, total;

  System.out.print("Please enter the number of in-town driving miles: ");
  townmiles = input.nextInt();

  System.out.print("Please enter the number of highway driving miles: ");
  highwaymiles = input.nextInt();

  double tgallons = townmiles / 21.5;
  double hgallons = highwaymiles / 26.8;
  double total1 = tgallons + hgallons;

  System.out.println("The total number of gallons required is: " + total1);
 }

}