week3_src/DoWhileLoopFactorial.javaweek3_src/DoWhileLoopFactorial.javapackage edu.drexel.ct290;
import java.util.Scanner;
publicclassDoWhileLoopFactorial{
/**
* A factorial is calculated by multiplying a number by
* every integer less than itself except zero. For example the
* factorial of 4, written 4!, is 4*3*2*1 = 24.
*/
publiclong calculateFactorial(int number){
// declare and initialize a variable to hold the answer
long factorial=number;
// the do while loop will run the block of code between the braces
// as long as the condition in the while statement is true.
// The different between this and the while loop is that
// here, the code will always execute at least once.
do{
// the print statement can help debug errors is in the code
System.out.println("Fact: "+ factorial +", number: "+ number);
// calculate the next factor
factorial = factorial *(number-1);
// decrement the number so that the next iteration of the loop
// will have the correct value to multiply
number--;
}
while( number >1);
// return the answer to the caller.
return factorial;
}
publicstaticvoid main(String[] args){
// Get the user input
Scanner reader =newScanner(System.in);
System.out.print("What number do you want a factorial for: ");
int number = reader.nextInt();
// Create a DoWhileLoopFactorial class
DoWhileLoopFactorial loopFactorial =newDoWhileLoopFactorial();
// call calculateFactorial to compute the answer
long factorial = loopFactorial.calculateFactorial( number );
// Show the user the answer
System.out.println("The answer is: "+ factorial);
}
}
__MACOSX/week3_src/._DoWhileLoopFactorial.java
week3_src/ForLoopFactorial.javaweek3_src/ForLoopFactorial.javapackage edu.drexel.ct290;
import java.util.Scanner;
publicclassForLoopFactorial{
/**
* A factorial is calculated by multiplying a number by
* every integer less than itself except zero. For example the
* factorial of 4, written 4!, is 4*3*2*1 = 24.
*/
publiclong calculateFactorial(int number){
// declare and initialize a variable to hold the answer
long factorial = number;
// The for loop has three parts:
// initialization: int i=number;
// condition: i>1;
// increment/decrement: i--;
// The variable in the initialization is called the control variable
// The initialization happens once when the loop starts.
// The loop will execute as long as the condition is true.
// The decrement will happen automatically after each iteration.
for(int i=number; i>1; i--){
// the print statement can help debug errors is in the code
System.out.println("Fact: "+ factorial +", i: "+ i);
// calculate the next factor
factorial = factorial *(i-1);
}
// return the answer to the caller.
return factorial;
}
publicstaticvoid main(String[] args){
// Get the user input
Scanner reader =newScanner(System.in);
System.out.print("What number do you want a factorial for: ");
int number = reader.nextInt();
// Create a WhileLoopFactoria.
1. week3_src/DoWhileLoopFactorial.javaweek3_src/DoWhileLoop
Factorial.javapackage edu.drexel.ct290;
import java.util.Scanner;
publicclassDoWhileLoopFactorial{
/**
* A factorial is calculated by multiplying a number by
* every integer less than itself except zero. For example the
* factorial of 4, written 4!, is 4*3*2*1 = 24.
*/
publiclong calculateFactorial(int number){
// declare and initialize a variable to hold the answer
long factorial=number;
// the do while loop will run the block of code between the brac
es
// as long as the condition in the while statement is true.
// The different between this and the while loop is that
// here, the code will always execute at least once.
do{
// the print statement can help debug errors is in the code
System.out.println("Fact: "+ factorial +", number: "+ number);
// calculate the next factor
factorial = factorial *(number-1);
// decrement the number so that the next iteration of the loop
// will have the correct value to multiply
number--;
}
2. while( number >1);
// return the answer to the caller.
return factorial;
}
publicstaticvoid main(String[] args){
// Get the user input
Scanner reader =newScanner(System.in);
System.out.print("What number do you want a factorial for: ");
int number = reader.nextInt();
// Create a DoWhileLoopFactorial class
DoWhileLoopFactorial loopFactorial =newDoWhileLoopFactori
al();
// call calculateFactorial to compute the answer
long factorial = loopFactorial.calculateFactorial( number );
// Show the user the answer
System.out.println("The answer is: "+ factorial);
}
}
__MACOSX/week3_src/._DoWhileLoopFactorial.java
week3_src/ForLoopFactorial.javaweek3_src/ForLoopFactorial.j
avapackage edu.drexel.ct290;
import java.util.Scanner;
publicclassForLoopFactorial{
/**
3. * A factorial is calculated by multiplying a number by
* every integer less than itself except zero. For example the
* factorial of 4, written 4!, is 4*3*2*1 = 24.
*/
publiclong calculateFactorial(int number){
// declare and initialize a variable to hold the answer
long factorial = number;
// The for loop has three parts:
// initialization: int i=number;
// condition: i>1;
// increment/decrement: i--;
// The variable in the initialization is called the control variable
// The initialization happens once when the loop starts.
// The loop will execute as long as the condition is true.
// The decrement will happen automatically after each iteration.
for(int i=number; i>1; i--){
// the print statement can help debug errors is in the code
System.out.println("Fact: "+ factorial +", i: "+ i);
// calculate the next factor
factorial = factorial *(i-1);
}
// return the answer to the caller.
return factorial;
}
publicstaticvoid main(String[] args){
// Get the user input
Scanner reader =newScanner(System.in);
System.out.print("What number do you want a factorial for: ");
int number = reader.nextInt();
// Create a WhileLoopFactorial class
4. ForLoopFactorial loopFactorial =newForLoopFactorial();
// call calculateFactorial to compute the answer
long factorial = loopFactorial.calculateFactorial( number );
// Show the user the answer
System.out.println("The answer is: "+ factorial);
}
}
__MACOSX/week3_src/._ForLoopFactorial.java
week3_src/GradeBookEntry.javaweek3_src/GradeBookEntry.jav
apackage edu.drexel.ct290;
import java.util.Scanner;
import edu.drexel.ct290.solution.GradeConverter;
publicclassGradeBookEntry{
privatePerson student;
privateint numericGrade;
privateString assessmentName;
// The next six methods are just getters and setters
// for the member variables of this class.
publicPerson getStudent(){
return student;
}
publicvoid setStudent(Person student){
this.student = student;
}
5. publicint getNumericGrade(){
return numericGrade;
}
publicvoid setNumericGrade(int numericGrade){
this.numericGrade = numericGrade;
}
publicString getAssessmentName(){
return assessmentName;
}
publicvoid setAssessmentName(String assessmentName){
this.assessmentName = assessmentName;
}
publicvoid printEntry(){
System.out.println(student.toString());
// instantiate a GradeConverter to get the letter grade.
GradeConverter converter =newGradeConverter();
System.out.println("Scored "+ numericGrade );
System.out.println("Which is a: "+ converter.convertGrade(num
ericGrade));
System.out.println("For assessment: "+ assessmentName);
}
publicstaticvoid main(String[] args){
// instantiate the GradeBookEntry, just like we have
// done with the Scanner class.
GradeBookEntry gradeBookEntry =newGradeBookEntry();
Scanner reader =newScanner(System.in);
// instantiate a new person object
Person student =newPerson();
student.getPersonData();
6. gradeBookEntry.setStudent(student);
System.out.print("Enter this students numeric grade: ");
int grade = reader.nextInt();
gradeBookEntry.setNumericGrade(grade);
gradeBookEntry.setAssessmentName("test1");
gradeBookEntry.printEntry();
}
}
__MACOSX/week3_src/._GradeBookEntry.java
week3_src/GradeConverter.javaweek3_src/GradeConverter.java
package edu.drexel.ct290;
publicclassGradeConverter{
publicString convertGrade(int numberGrade ){
if( numberGrade >89){
return"A";
}
//else if...
else{
}
// TODO: fill in the rest of this method.
// Use else if statements and else to finish
// the grade conversion.
}
/**
7. * This method gets input from the user
* @return a grade in number format from 0-100
*/
publicint getNumberGrade(){
int userInput=0;
// TODO complete this method
return userInput;
}
/**
* @param args
*/
publicstaticvoid main(String[] args){
GradeConverter converter =newGradeConverter();
int input = converter.getNumberGrade();
String letterGrade = converter.convertGrade(input);
System.out.println("The letter grade for "+ input +" is "+ letter
Grade);
}
}
__MACOSX/week3_src/._GradeConverter.java
week3_src/GuessTheNumber.javaweek3_src/GuessTheNumber.j
avapackage edu.drexel.ct290;
import java.util.Random;
import java.util.Scanner;
publicclassGuessTheNumber{
publicint getRandomNumber(int range ){
// Instantiate a random number generator
Random rand =newRandom();
8. //Generate a number in the given range
int answer = rand.nextInt(range)+1;
return answer;
}
publicint getUserGuess(){
System.out.print("Enter your guess: ");
Scanner reader =newScanner(System.in);
return reader.nextInt();
}
publicboolean compare(int guess,int answer){
/* TODO: Check the answer. Return true if correct,
* otherwise return false and give an indication if
* the answer was too high or too low
*/
if(/* correct condition */){
returntrue;
}
// TODO: fill in the code to tell if they are too high or too low
returnfalse;
}
publicstaticvoid main(String[] args){
System.out.println("Lets play game. I'll pick a number 1-
100 and you guess.");
GuessTheNumber guessNumber =newGuessTheNumber();
int answer = guessNumber.getRandomNumber(100);
int guess = guessNumber.getUserGuess();
9. // Write a loop that keeps asking the user to guess
// until they get it right.
System.out.println("Congratulations! You got it: "+ answer);
}
}
__MACOSX/week3_src/._GuessTheNumber.java
week3_src/Instructions.docx
ASSIGNMENT: GUESSING GAME
· Using GuessTheNumber.java as a base, write a guessing game
program.
· The computer should pick a number and the user will guess.
· The user should be able to guess until they win.
· The computer will tell the user if their guess is too high or too
low.
· See Java HTP 6.9 to learn more about Java's Random class.
__MACOSX/week3_src/._Instructions.docx
week3_src/LogicalOperators.javaweek3_src/LogicalOperators.ja
vapackage edu.drexel.ct290;
import java.util.Scanner;
publicclassLogicalOperators{
privatefinalint MALE =1;
publicboolean getInput(String question ){
Scanner reader =newScanner(System.in);
10. System.out.println(question);
System.out.print(">");
// to enter a boolean you must type in true or false in the consol
e
return reader.nextBoolean();
}
publicstaticvoid main(String[] args){
LogicalOperators logicalOps =newLogicalOperators();
boolean condition1;
boolean condition2;
System.out.println();
condition1 = logicalOps.getInput("Is the first condition tru
e or false?");
condition2 = logicalOps.getInput("Is the second condition
true or false?");
// if condition1 is false, condition2 will not even be evaluated
if( condition1 && condition2 ){
System.out.println("Both conditions are true");
}
if( condition1 || condition2 ){
System.out.println("At least one of your conditions are true");
}
if(!condition1 ){
System.out.println("Condition1 is NOT true.");
}
// This says: if condition 1 and 2 are NOT both true
if(!(condition1 && condition2)){
System.out.println("At least one of your conditions are false");
11. }
// This is another way of expressing the same thing.
if(!condition1 ||!condition2 ){
System.out.println("At least one of your conditions are false");
}
}
}
__MACOSX/week3_src/._LogicalOperators.java
week3_src/LoopInput.javaweek3_src/LoopInput.javapackage ed
u.drexel.ct290;
import java.util.Scanner;
publicclassLoopInput{
publicstaticvoid main(String[] args){
Scanner reader =newScanner(System.in);
int userInput =1;
while(userInput !=0){
System.out.println("Enter two numbers for me to add:");
System.out.print("First Number: ");
int a = reader.nextInt();
System.out.print("Second Number: ");
int b = reader.nextInt();
int answer = a + b;
System.out.println("The answer is: "+ answer);
System.out.println("Press 1 to add more numbers, or 0 to exit:")
13. }
publicString getEmail(){
return email;
}
publicvoid setEmail(String email){
this.email = email;
}
publicvoid getPersonData(){
Scanner reader =newScanner(System.in);
System.out.print("Enter the person's name: ");
name = reader.nextLine();
System.out.print("Enter the person's age: ");
age = reader.nextInt();
reader.nextLine();
System.out.print("Enter the person's email: ");
email = reader.nextLine();
}
publicString toString(){
return"Name: "+ name +"nAge: "+ age +"nemail: "+ email;
}
}
__MACOSX/week3_src/._Person.java
week3_src/WhileLoopFactorial.javaweek3_src/WhileLoopFacto
rial.javapackage edu.drexel.ct290;
import java.util.Scanner;
14. publicclassWhileLoopFactorial{
/**
* A factorial is calculated by multiplying a number by
* every integer less than itself except zero. For example the
* factorial of 4, written 4!, is 4*3*2*1 = 24.
*/
publiclong calculateFactorial(int number){
// declare and initialize a variable to hold the answer
long factorial=number;
// the while loop will run the block of code between the braces
// as long as the condition in parenthesis is true.
// If the condition is false right off the bat, then
// the loop code will never be executed at all.
while( number >1){
// the print statement can help debug errors is in the code
System.out.println("Fact: "+ factorial +", number: "+ number);
// calculate the next factor
factorial = factorial *(number-1);
// decrement the number so that the next iteration of the loop
// will have the correct value to multiply
number--;
}
// return the answer to the caller.
return factorial;
}
publicstaticvoid main(String[] args){
// Get the user input
15. Scanner reader =newScanner(System.in);
System.out.print("What number do you want a factorial for: ");
int number = reader.nextInt();
// Create a WhileLoopFactorial class
WhileLoopFactorial loopFactorial =newWhileLoopFactorial();
// call calculateFactorial to compute the answer
long factorial = loopFactorial.calculateFactorial( number );
// Show the user the answer
System.out.println("The answer is: "+ factorial);
}
}
__MACOSX/week3_src/._WhileLoopFactorial.java
__MACOSX/._week3_src