我的Access作業第一次被老師扣了分數,原因是我在畫兩個table的relationship的時候,有一個table的主鍵(Primary Key)不知道什麼時候不小心被我取消掉了,所以被扣分。只能說自己對Access的敏感度不夠,主鍵那麼重要的東西應該一眼就要看到,以後可得注意一點啦!
Showing posts with label homework. Show all posts
Showing posts with label homework. Show all posts
5/06/2011
5/05/2011
Guess the Number Game
import java.util.*;
public class GuessNumberGame
{
public static void main(String[] args)
{
System.out.println("Welcome to the Guess the Number Game" + "\n" +
"++++++++++++++++++++++++++++++++++++");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
System.out.println("I'm thinking of a number from 1 to 100"
+ "\n" + "Try to guess it." + "\n");
int randomNumber = (int)(Math.random() * 99 + 1);
int numberOfGuesses = 0;
// print out the random Number to check the game
// hide this code before turning homework
System.out.println("The answer from the system is: " + randomNumber);
// format the result as a single string
while (true)
{
int guess = getIntWithinRange(sc,"Please input valid number." + "\n",1,100);
numberOfGuesses++;
if (guess == randomNumber) // use "==" not "="
{
if (numberOfGuesses <=3)
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "Great work You are a mathematical wizard." + "\n");
else if ((numberOfGuesses > 3) && (numberOfGuesses <= 7))
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "Not too bad! You've got some potential."+ "\n");
else if (numberOfGuesses >7)
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "What took you so long? Maybe you should " +
"take some lessons."+ "\n");
break;
}
else
{
if (guess >= (randomNumber + 10))
System.out.println("Way too high! Please guess again." + "\n");
else if (guess > randomNumber)
System.out.println("Too high! Please guess again." + "\n");
else if (guess < randomNumber)
System.out.println("Too low! Please guess again." + "\n");
}
}
// see if the user wants to continue
while (true)
{
System.out.print("Try again? (y/n)");
choice = sc.next();
System.out.println();
if (choice.equals("y") || choice.equals("n"))
{
if (choice.equals("y"))
System.out.println("The new game starts." + "\n");
else if (choice.equals("n"))
System.out.println("Bye - Come back soon!");
break;
}
else if ((!choice.equalsIgnoreCase("y")) && (!choice.equalsIgnoreCase("n")))
{
if (choice.equals(""))
{
System.out.println("Error! This entry is required. Try again");
}
else
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
}
}
}
}
public static int getIntWithinRange(Scanner sc, String prompt,int min, int max)
{
int i;
while(true)
{
System.out.print("Enter Nubmer:");
i = sc.nextInt();
if(i<min || i>max)
{
System.out.println(prompt);
}
else
break;
}
return i;
}
}
public class GuessNumberGame
{
public static void main(String[] args)
{
System.out.println("Welcome to the Guess the Number Game" + "\n" +
"++++++++++++++++++++++++++++++++++++");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
System.out.println("I'm thinking of a number from 1 to 100"
+ "\n" + "Try to guess it." + "\n");
int randomNumber = (int)(Math.random() * 99 + 1);
int numberOfGuesses = 0;
// print out the random Number to check the game
// hide this code before turning homework
System.out.println("The answer from the system is: " + randomNumber);
// format the result as a single string
while (true)
{
int guess = getIntWithinRange(sc,"Please input valid number." + "\n",1,100);
numberOfGuesses++;
if (guess == randomNumber) // use "==" not "="
{
if (numberOfGuesses <=3)
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "Great work You are a mathematical wizard." + "\n");
else if ((numberOfGuesses > 3) && (numberOfGuesses <= 7))
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "Not too bad! You've got some potential."+ "\n");
else if (numberOfGuesses >7)
System.out.println("You got it in " + numberOfGuesses + " tries."
+ "\n" + "What took you so long? Maybe you should " +
"take some lessons."+ "\n");
break;
}
else
{
if (guess >= (randomNumber + 10))
System.out.println("Way too high! Please guess again." + "\n");
else if (guess > randomNumber)
System.out.println("Too high! Please guess again." + "\n");
else if (guess < randomNumber)
System.out.println("Too low! Please guess again." + "\n");
}
}
// see if the user wants to continue
while (true)
{
System.out.print("Try again? (y/n)");
choice = sc.next();
System.out.println();
if (choice.equals("y") || choice.equals("n"))
{
if (choice.equals("y"))
System.out.println("The new game starts." + "\n");
else if (choice.equals("n"))
System.out.println("Bye - Come back soon!");
break;
}
else if ((!choice.equalsIgnoreCase("y")) && (!choice.equalsIgnoreCase("n")))
{
if (choice.equals(""))
{
System.out.println("Error! This entry is required. Try again");
}
else
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
}
}
}
}
public static int getIntWithinRange(Scanner sc, String prompt,int min, int max)
{
int i;
while(true)
{
System.out.print("Enter Nubmer:");
i = sc.nextInt();
if(i<min || i>max)
{
System.out.println(prompt);
}
else
break;
}
return i;
}
}
4/28/2011
Greatest Common Divisor
話說這回的作業是上研究所課程以來第三次的程式作業,內容只有這一小部份跟之前不一樣,卻大大加長了我寫爪哇作業的時間,只能說接下來的這一個半月還得跟爪哇做好朋友,爪哇~我要每天多認識你一點一點呀!
這次作業主要是請玩家輸入兩個數字,然後求這兩個入字的最大公因數。
// import class
import java.util.Scanner;
public class CH04PR43
{
public static void main(String[] args)
{
// Welcome the user to come to the program
System.out.println("Greatest Common Divisor Finder");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// Get numbers from the user
System.out.print("Enter first number: ");
int firstNumber = sc.nextInt();
System.out.print("Enter second number: ");
int secondNumber = sc.nextInt();
// do algorithm
int temp = 0;
while (secondNumber!=0)
{
temp = firstNumber % secondNumber;
firstNumber = secondNumber;
secondNumber = temp;
}
System.out.println("Greatest Common Divisor: " + firstNumber);
System.out.println();
// see if the user want to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}
這次作業主要是請玩家輸入兩個數字,然後求這兩個入字的最大公因數。
// import class
import java.util.Scanner;
public class CH04PR43
{
public static void main(String[] args)
{
// Welcome the user to come to the program
System.out.println("Greatest Common Divisor Finder");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// Get numbers from the user
System.out.print("Enter first number: ");
int firstNumber = sc.nextInt();
System.out.print("Enter second number: ");
int secondNumber = sc.nextInt();
// do algorithm
int temp = 0;
while (secondNumber!=0)
{
temp = firstNumber % secondNumber;
firstNumber = secondNumber;
secondNumber = temp;
}
System.out.println("Greatest Common Divisor: " + firstNumber);
System.out.println();
// see if the user want to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}
4/21/2011
Travel Time Calculator
這是我上禮拜的Java作業,滿分十分,老師給了九分。
以下是我寫的code
// import class
import java.util.Scanner;
public class CH03PR32
{
public static void main(String[] args)
{
// Welcome the user to come to the program
System.out.println("Welcome to the Travel Time Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get input numbers from the user
System.out.print("Enter miles: ");
double miles = sc.nextDouble();
System.out.print("Enter miles per hour: ");
double milesPerHour = sc.nextDouble();
System.out.println();
// show the estimated travel time
System.out.println("Estimated travel time");
// calculate estimated hours and minutes
double hours = miles / milesPerHour ;
hours = Math.floor(hours);
int estimatedhours = (int)hours;
double minutes = 60 * (miles % milesPerHour) / milesPerHour ;
minutes = Math.floor(minutes);
int estimatedminutes = (int)minutes;
System.out.println("Hours: " + estimatedhours);
System.out.println("Minutes: " + estimatedminutes);
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next(); // refer to Scanner
System.out.println();
}
}
}
以下是我寫的code
// import class
import java.util.Scanner;
public class CH03PR32
{
public static void main(String[] args)
{
// Welcome the user to come to the program
System.out.println("Welcome to the Travel Time Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get input numbers from the user
System.out.print("Enter miles: ");
double miles = sc.nextDouble();
System.out.print("Enter miles per hour: ");
double milesPerHour = sc.nextDouble();
System.out.println();
// show the estimated travel time
System.out.println("Estimated travel time");
// calculate estimated hours and minutes
double hours = miles / milesPerHour ;
hours = Math.floor(hours);
int estimatedhours = (int)hours;
double minutes = 60 * (miles % milesPerHour) / milesPerHour ;
minutes = Math.floor(minutes);
int estimatedminutes = (int)minutes;
System.out.println("Hours: " + estimatedhours);
System.out.println("Minutes: " + estimatedminutes);
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next(); // refer to Scanner
System.out.println();
}
}
}
4/07/2011
Number letter grade converter
The Java assignment this week is number letter grades converter, which means to make numerical grades into letter grades. For example, 90-100 points may refer to A, 80-89 refers to B and so on.
I read first two chapters and took two hours to do my homework. I will post my code here after professor editing my homework.
In Double, decimal number (ex. 87.5) can refer to whole number (ex. 87). However, in Int, whole number (ex. 87) doesn't refer to decimal number (ex. 87.5). Therefore, I can not use Int here.
My code are as follows,
And the teacher's comment are as follows.
In order to import the application successfully, it was first necessary to change both the package statement and the name of the class. Please use package and class names specified for assignments. After those adjustments, the application ran fine and produced correct results for the different tests I tried.
I read first two chapters and took two hours to do my homework. I will post my code here after professor editing my homework.
In Double, decimal number (ex. 87.5) can refer to whole number (ex. 87). However, in Int, whole number (ex. 87) doesn't refer to decimal number (ex. 87.5). Therefore, I can not use Int here.
My code are as follows,
package chapt02;
import java.util.Scanner;
public class LetterGradeConverter // create class and no space between each word
{
public static void main(String[] args)
{
// welcome the user to come to the program
System.out.println("Welcome to the Letter Grade Converter"); // do not forget ;
System.out.println(); // print blank line
// create a Scanner object name sc
Scanner sc = new Scanner(System.in); // S is uppercase letter
// perform letter grade converter until choice is not "y" or "Y"
String choice = "y"; // do not forget ;
while (choice.equalsIgnoreCase("y")) // not case-sensitive
{
// get the number grades from the user
System.out.print("Enter numerical grades: ");
double numbergrade = sc.nextDouble(); // D in nextDouble is uppercase
char lettergrade = 0;
// convert number grades to letter grades
if (numbergrade >= 88)
lettergrade = 'A';
else if (numbergrade >= 80)
lettergrade = 'B';
else if (numbergrade >= 67)
lettergrade = 'C';
else if (numbergrade >= 60)
lettergrade = 'D'; // do not forget ; to end the code
else
lettergrade = 'F';
System.out.println("Letter grade: " + lettergrade); // do not forget ; to end the code
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next(); // refer to Scanner object
System.out.println();
}
}
}
And the teacher's comment are as follows.
In order to import the application successfully, it was first necessary to change both the package statement and the name of the class. Please use package and class names specified for assignments. After those adjustments, the application ran fine and produced correct results for the different tests I tried.
Subscribe to:
Posts (Atom)