Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. September 10, 2009
Homework 1
Eugene Agichtein (eugene@mathcs.emory.edu)
Slawek Goryczka (sgorycz@emory.edu)
Liyue Fan (lfan3@emory.edu)
This homework consists of writing 4 simple programs and answering some questions about
them. The turnin will require the files TestDivide.java, CrazyDoubles.java, LetterGrade.java,
and BlackJack.java be placed in your ~/cs170/hw1 directory. We suggest testing these
programs on your own by java classname before turnin. The deadline is midnight of
September 23.
Please make sure that you have added following statement at the top of each source code
file.
/*
THIS CODE IS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING
A TUTOR OR CODE WRITTEN BY OTHER STUDENTS. Your Name
*/
You will not be able to turn in without that statement.
How to create a Java Program and turnin an assignment
In this homework you will need to create several Java Programs, and then turn them in.
This example walks you through the process of creating a Java program called Hello, mak-
ing sure it is stored in the correct directory on the file system for hw1 and running the turnin
script to turn in your work. It assumes you are on a machine at the lab, and have a terminal
window open.
1. Create the hw1 directory inside your cs170 directory. (mkdir ~/cs170/hw1)
2. Set the current working directory to the hw1 directory you just created. (cd ~/cs170/hw1)
3. Create and edit a java file named according to the program you are creating. (gedit Hello.java &).
4. Insert the following text into the file:
public class Hello{
public static void main(String[] args){
System.out.println("Hello World.");
}
}
5. Save the file inside the editor. (Click on menu: File->Save).
6. Compile the java program. (javac Hello.java)
7. Run the program to test it. (java Hello)
8. Turn in the assignment. Note this will not work until the entire assignment is complete.
(~cs170000/turnin hw1)
1
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. September 10, 2009
Program 1
Please look at the following segment of code:
int x = 3;
int y = 2;
boolean b = ((x/y) > 1) && ((x/y) < 2);
if(b){
System.out.println("The world seems normal to me.");
}
else{
System.out.println("There is something fishy here");
}
Write a Java program called TestDivide that tests the code above. Then modify only
ONE of the first TWO lines of the program so that b evaluates to true. Include in a
comment at the bottom of your code the answer to the question:
How does your modification change the execution of the code?
Program 2
Please look at the following segment of code:
double x = 0.1;
double y = 0.1;
double z = 0.1;
if(x+y+z==0.3){
System.out.println("The world seems normal to me.");
}
else{
System.out.println("There is something fishy here");
}
Write a program called CrazyDoubles that tests the code above. Include in a comment
at the bottom of your code the answers to the questions:
1 What is printed out when you run the program?
2 Why does the code print that out?
2
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. September 10, 2009
Program 3
Mrs. Sullivan has a 6th Grade Class with an exam worth 175 points. She wishes to assign
letter grades according to the following percentages:
100%-90% A
89%-80% B
79%-70% C
69%-60% D
59%-0% F
Note: Mrs. Sullivan believes in truncating instead of rounding. Therefore, an 89.9% is
truncated to an 89% which is a B.
Write a Java program called LetterGrade that takes as input (use the Scanner) the
number of points the student got on the exam, and outputs the appropriate letter grade.
You may assume the input will always be between 0 -175. To get maximum credit you must
use only 4 numeric comparisons in your code (Hint: Use else if). Include in a comment
at the bottom of your code the answers to the questions:
1 What is the maximum number of comparisons that might be executed in your code?
(Hint: Think of different possible inputs and how many comparisons your code will
execute for each).
2 What is the minimum number of comparisons that might be executed in your code?
Example usage:
=====================
java LetterGrade
Please specify the numeric grade.
139
C
====================
Note: The only input should be the numeric score. The LAST line of output must be the
appropriate UPPER CASE letter grade.
3
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. September 10, 2009
Program 4
In the Anglo-American playing card deck there are 52 cards with 13 different values and 4
different suits. The values of the cards are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.
Often in computer programs these values are represented by an integer in the range from 1
to 13 respectively.
In the card game BlackJack, the hand with the highest total wins as long as it doesn’t
exceed 21; a hand with a higher total than 21 is said to bust or be too many. Cards 2
through 10 are worth their face value, and face cards (jack, queen, king) are all worth 10.
An ace’s value is 11 unless this would cause the player to bust, in which case it is worth 1.
A hand in which an ace’s value is counted as 11 is called a soft hand, because it cannot be
busted if the player draws another card.
Write a program called BlackJack which takes 3 integer inputs (ranging from 1-13)
representing playing cards and outputs one of the three phrases below:
This hand is a bust.
This hand is a soft X.
This hand is a X.
The program must output the appropriate phrase and replace X with the total score of
the hand.
Example usage:
=====================
java BlackJack
Please specify 3 playing cards as numbers in the range from 1-13 representing A-K.
1
7
2
This hand is a soft 20.
====================
Note: The only input should be 3 numbers. The LAST line of output must be ONE of the
3 phrases above.
4