مشكور ع التوووضيح شرح ممتاز...
ممكن حل هذا السوال مافهمته...
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: as an example
Problem Analysis Chart:
Given Data
Required Results
name
color
animal
food
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 deion 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 said
it 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 + "!");
}
}