SlideShare a Scribd company logo
1 of 15
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--;
}
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{
/**
* 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
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;
}
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();
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.
}
/**
* 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();
//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();
// 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);
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");
}
// 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:")
;
userInput = reader.nextInt();
}
System.out.print("Good bye!");
}
}
__MACOSX/week3_src/._LoopInput.java
week3_src/Person.javaweek3_src/Person.javapackage edu.drexe
l.ct290;
import java.util.Scanner;
publicclassPerson{
privateString name;
privateint age;
privateString email;
publicString getName(){
return name;
}
publicvoid setName(String name){
this.name = name;
}
publicint getAge(){
return age;
}
publicvoid setAge(int age){
this.age = age;
}
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;
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
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

More Related Content

Similar to week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx

Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgalyssa-castro2326
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresGowtham Reddy
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingPathomchon Sriwilairit
 
Ive posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfIve posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfdeepaarora22
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docxransayo
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docxjoyjonna282
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfFashionBoutiquedelhi
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 

Similar to week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx (20)

Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prg
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphores
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Ive posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfIve posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdf
 
BLM101_2.pptx
BLM101_2.pptxBLM101_2.pptx
BLM101_2.pptx
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docx
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 

More from alanfhall8953

With regards to this article, I agree and disagree on certain leve.docx
With regards to this article, I agree and disagree on certain leve.docxWith regards to this article, I agree and disagree on certain leve.docx
With regards to this article, I agree and disagree on certain leve.docxalanfhall8953
 
WIT Financial Accounting Test Ch.docx
WIT                   Financial Accounting Test                 Ch.docxWIT                   Financial Accounting Test                 Ch.docx
WIT Financial Accounting Test Ch.docxalanfhall8953
 
Windows Server Deployment ProposalOverviewEach student will .docx
Windows Server Deployment ProposalOverviewEach student will .docxWindows Server Deployment ProposalOverviewEach student will .docx
Windows Server Deployment ProposalOverviewEach student will .docxalanfhall8953
 
Wireshark Lab TCP v6.0 Supplement to Computer Networking.docx
Wireshark Lab TCP v6.0  Supplement to Computer Networking.docxWireshark Lab TCP v6.0  Supplement to Computer Networking.docx
Wireshark Lab TCP v6.0 Supplement to Computer Networking.docxalanfhall8953
 
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docx
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docxWireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docx
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docxalanfhall8953
 
Wireshark Lab IP v6.0 Supplement to Computer Networking.docx
Wireshark Lab IP v6.0  Supplement to Computer Networking.docxWireshark Lab IP v6.0  Supplement to Computer Networking.docx
Wireshark Lab IP v6.0 Supplement to Computer Networking.docxalanfhall8953
 
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docx
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docxWillowbrook SchoolBackgroundWillowbrook School is a small, pri.docx
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docxalanfhall8953
 
Wind PowerUsed For Millennia Variations in alb.docx
Wind PowerUsed For Millennia Variations in alb.docxWind PowerUsed For Millennia Variations in alb.docx
Wind PowerUsed For Millennia Variations in alb.docxalanfhall8953
 
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docx
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docxwinter 2013 235 CREATE A CONTRACTInstructionsI will giv.docx
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docxalanfhall8953
 
WinEst As 1. Es2. Tassignment stInfo (Esti.docx
WinEst As 1. Es2. Tassignment stInfo (Esti.docxWinEst As 1. Es2. Tassignment stInfo (Esti.docx
WinEst As 1. Es2. Tassignment stInfo (Esti.docxalanfhall8953
 
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docx
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docxWiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docx
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docxalanfhall8953
 
Winter 2011 • Morality in Education 35Workplace Bullying .docx
Winter 2011 • Morality in Education 35Workplace Bullying .docxWinter 2011 • Morality in Education 35Workplace Bullying .docx
Winter 2011 • Morality in Education 35Workplace Bullying .docxalanfhall8953
 
With the competitive advantage that Crocs’ supply chain holds, the.docx
With the competitive advantage that Crocs’ supply chain holds, the.docxWith the competitive advantage that Crocs’ supply chain holds, the.docx
With the competitive advantage that Crocs’ supply chain holds, the.docxalanfhall8953
 
Windows Server 2012 R2 Essentials Windows Server 2012.docx
Windows Server 2012 R2 Essentials  Windows Server 2012.docxWindows Server 2012 R2 Essentials  Windows Server 2012.docx
Windows Server 2012 R2 Essentials Windows Server 2012.docxalanfhall8953
 
Wind power resources on the eastern U.S. continental shelf are est.docx
Wind power resources on the eastern U.S. continental shelf are est.docxWind power resources on the eastern U.S. continental shelf are est.docx
Wind power resources on the eastern U.S. continental shelf are est.docxalanfhall8953
 
WilliamStearman_Java301build.xml Builds, tests, and ru.docx
WilliamStearman_Java301build.xml      Builds, tests, and ru.docxWilliamStearman_Java301build.xml      Builds, tests, and ru.docx
WilliamStearman_Java301build.xml Builds, tests, and ru.docxalanfhall8953
 
Wilco Corporation has the following account balances at December 3.docx
Wilco Corporation has the following account balances at December 3.docxWilco Corporation has the following account balances at December 3.docx
Wilco Corporation has the following account balances at December 3.docxalanfhall8953
 
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docx
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docxWilson Majee Technology Diffusion, S-Curve, and Innovation.docx
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docxalanfhall8953
 
WinARM - Simulating Advanced RISC Machine Architecture .docx
WinARM - Simulating Advanced RISC Machine Architecture   .docxWinARM - Simulating Advanced RISC Machine Architecture   .docx
WinARM - Simulating Advanced RISC Machine Architecture .docxalanfhall8953
 
William PennWhat religion was William PennWilliam Pen was fr.docx
William PennWhat religion was William PennWilliam Pen was fr.docxWilliam PennWhat religion was William PennWilliam Pen was fr.docx
William PennWhat religion was William PennWilliam Pen was fr.docxalanfhall8953
 

More from alanfhall8953 (20)

With regards to this article, I agree and disagree on certain leve.docx
With regards to this article, I agree and disagree on certain leve.docxWith regards to this article, I agree and disagree on certain leve.docx
With regards to this article, I agree and disagree on certain leve.docx
 
WIT Financial Accounting Test Ch.docx
WIT                   Financial Accounting Test                 Ch.docxWIT                   Financial Accounting Test                 Ch.docx
WIT Financial Accounting Test Ch.docx
 
Windows Server Deployment ProposalOverviewEach student will .docx
Windows Server Deployment ProposalOverviewEach student will .docxWindows Server Deployment ProposalOverviewEach student will .docx
Windows Server Deployment ProposalOverviewEach student will .docx
 
Wireshark Lab TCP v6.0 Supplement to Computer Networking.docx
Wireshark Lab TCP v6.0  Supplement to Computer Networking.docxWireshark Lab TCP v6.0  Supplement to Computer Networking.docx
Wireshark Lab TCP v6.0 Supplement to Computer Networking.docx
 
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docx
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docxWireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docx
Wireshark Lab IP v6.0Supplement to Computer Networking A Top-D.docx
 
Wireshark Lab IP v6.0 Supplement to Computer Networking.docx
Wireshark Lab IP v6.0  Supplement to Computer Networking.docxWireshark Lab IP v6.0  Supplement to Computer Networking.docx
Wireshark Lab IP v6.0 Supplement to Computer Networking.docx
 
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docx
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docxWillowbrook SchoolBackgroundWillowbrook School is a small, pri.docx
Willowbrook SchoolBackgroundWillowbrook School is a small, pri.docx
 
Wind PowerUsed For Millennia Variations in alb.docx
Wind PowerUsed For Millennia Variations in alb.docxWind PowerUsed For Millennia Variations in alb.docx
Wind PowerUsed For Millennia Variations in alb.docx
 
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docx
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docxwinter 2013 235 CREATE A CONTRACTInstructionsI will giv.docx
winter 2013 235 CREATE A CONTRACTInstructionsI will giv.docx
 
WinEst As 1. Es2. Tassignment stInfo (Esti.docx
WinEst As 1. Es2. Tassignment stInfo (Esti.docxWinEst As 1. Es2. Tassignment stInfo (Esti.docx
WinEst As 1. Es2. Tassignment stInfo (Esti.docx
 
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docx
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docxWiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docx
Wiley Plus Brief Exercise 6 –Accounting 100Brief Exercise 6-1B.docx
 
Winter 2011 • Morality in Education 35Workplace Bullying .docx
Winter 2011 • Morality in Education 35Workplace Bullying .docxWinter 2011 • Morality in Education 35Workplace Bullying .docx
Winter 2011 • Morality in Education 35Workplace Bullying .docx
 
With the competitive advantage that Crocs’ supply chain holds, the.docx
With the competitive advantage that Crocs’ supply chain holds, the.docxWith the competitive advantage that Crocs’ supply chain holds, the.docx
With the competitive advantage that Crocs’ supply chain holds, the.docx
 
Windows Server 2012 R2 Essentials Windows Server 2012.docx
Windows Server 2012 R2 Essentials  Windows Server 2012.docxWindows Server 2012 R2 Essentials  Windows Server 2012.docx
Windows Server 2012 R2 Essentials Windows Server 2012.docx
 
Wind power resources on the eastern U.S. continental shelf are est.docx
Wind power resources on the eastern U.S. continental shelf are est.docxWind power resources on the eastern U.S. continental shelf are est.docx
Wind power resources on the eastern U.S. continental shelf are est.docx
 
WilliamStearman_Java301build.xml Builds, tests, and ru.docx
WilliamStearman_Java301build.xml      Builds, tests, and ru.docxWilliamStearman_Java301build.xml      Builds, tests, and ru.docx
WilliamStearman_Java301build.xml Builds, tests, and ru.docx
 
Wilco Corporation has the following account balances at December 3.docx
Wilco Corporation has the following account balances at December 3.docxWilco Corporation has the following account balances at December 3.docx
Wilco Corporation has the following account balances at December 3.docx
 
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docx
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docxWilson Majee Technology Diffusion, S-Curve, and Innovation.docx
Wilson Majee Technology Diffusion, S-Curve, and Innovation.docx
 
WinARM - Simulating Advanced RISC Machine Architecture .docx
WinARM - Simulating Advanced RISC Machine Architecture   .docxWinARM - Simulating Advanced RISC Machine Architecture   .docx
WinARM - Simulating Advanced RISC Machine Architecture .docx
 
William PennWhat religion was William PennWilliam Pen was fr.docx
William PennWhat religion was William PennWilliam Pen was fr.docxWilliam PennWhat religion was William PennWilliam Pen was fr.docx
William PennWhat religion was William PennWilliam Pen was fr.docx
 

Recently uploaded

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 

Recently uploaded (20)

Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 

week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx

  • 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:")
  • 12. ; userInput = reader.nextInt(); } System.out.print("Good bye!"); } } __MACOSX/week3_src/._LoopInput.java week3_src/Person.javaweek3_src/Person.javapackage edu.drexe l.ct290; import java.util.Scanner; publicclassPerson{ privateString name; privateint age; privateString email; publicString getName(){ return name; } publicvoid setName(String name){ this.name = name; } publicint getAge(){ return age; } publicvoid setAge(int age){ this.age = age;
  • 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