Exam Programer

Only available on StudyMode
  • Topic: Imperial units, United States customary units, Candy
  • Pages : 7 (1508 words )
  • Download(s) : 574
  • Published : October 7, 2012
Open Document
Text Preview
King Abdulaziz University
Faculty of Computing and Information Systems
Fall Term 2011/1432-1433

Course Code: CPCS-202Course Name: Programming I
________________________________________________________________

Problem 1:

Write a program that asks the user to enter a favorite color, a favorite food, a favorite animal, and the first name of a friend or relative. The program should then print the following two lines, with the user’s input replacing the items in italics: I had a dream that Name ate a Color Animal and said it tasted like Food!

For example, if the user entered blue for the color, hamburger for the food, dog for the animal, and Jake for the person’s name, the output would be I had a dream that Jake ate a blue dog and said it tasted like hamburger! Don’t forget to put the exclamation mark at the end.

Notes:

This project requires careful attention to spaces in output messages. In particular, a space must be explicitly printed between the variables color and animal.

Solution:

Problem Analysis Chart:

Given Data| Required Results|
namecoloranimalfood| Print “I had a dream that Name ate a Color Animal and said it tasted like Food!"| Processing Required| Solution Alternatives|
System.out.println("I had a dream that " + name + " ate a " + color + " " + animal); System.out.println("and said it tasted like " + food + "!");| |

Algorithm:

1. Read Name from user.
2. Read Color from user.
3. Read Animal from user.
4. Read Food from user.
5. Display message “I had a dream that Name ate a Color Animal and said it tasted like Food!"

Sample Code Solution:

From now on to the end of your four year program here at the University, every program you submit whether it is in lab or as homework should include the following: * File name
* Full description of the coded program
* Author: Name of student
* Student ID
* Student email address
* Date submitted

/** File name: SillySentence.java This program does the following: Prompts the user to enter a friend's or relative's name,a favorite color, a favorite food, and a favorite animal.Prints the following sentence with the user's input inserted:I had a dream that NAME ate a COLOR ANIMAL and saidit tasted like FOOD! Author: Lew Rakocy email address: LRakocy@devrycols.edu Date: 8/13/2000*/import java.util.*;public class SillySentence{ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the name of someone you know:"); String name = keyboard.nextLine(); System.out.println("Enter your favorite color: "); String color = keyboard.nextLine(); System.out.println("Enter your favorite food: "); String food = keyboard.nextLine(); System.out.println("Enter your favorite animal: "); String animal = keyboard.nextLine(); System.out.println("I had a dream that " + name + " ate a " + color + " " + animal); System.out.println("and said it tasted like " + food + "!"); }}|

Problem 2:

The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR.

The calories needed for a woman to maintain her weight is:
BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 * age in years)

The calories needed for a man to maintain his weight is:
BMR = 66 + (6.3 * weight in pounds) + (12.9 * height in inches) - (6.8 * age in years)

A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input their weight in pounds, height in inches, and age in years. The program should then output the number of chocolate bars that should be consumed to maintain one’s weight for both a woman and a man of the input weight, height, and age.

Notes:

This project...
tracking img