Java help

profilehoc34
week3_src.zip

week3_src/DoWhileLoopFactorial.java

week3_src/DoWhileLoopFactorial.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   DoWhileLoopFactorial   {
    
     /**
     * 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.
     */
     public   long  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 ;
     }

     public   static   void  main ( String []  args )   {
         // Get the user input
         Scanner  reader  =   new   Scanner ( System . in );
         System . out . print ( "What number do you want a factorial for: " );
         int  number  =  reader . nextInt ();
        
         // Create a DoWhileLoopFactorial class
         DoWhileLoopFactorial  loopFactorial  =   new   DoWhileLoopFactorial ();
        
         // 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.java

week3_src/ForLoopFactorial.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   ForLoopFactorial   {
    
     /**
     * 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.
     */
     public   long  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 ;
     }

     public   static   void  main ( String []  args )   {
         // Get the user input
         Scanner  reader  =   new   Scanner ( System . in );
         System . out . print ( "What number do you want a factorial for: " );
         int  number  =  reader . nextInt ();
                
         // Create a WhileLoopFactorial class
         ForLoopFactorial  loopFactorial  =   new   ForLoopFactorial ();
        
         // 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.java

week3_src/GradeBookEntry.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;
import  edu . drexel . ct290 . solution . GradeConverter ;

public   class   GradeBookEntry   {

     private   Person  student ;
     private   int  numericGrade ;
     private   String  assessmentName ;

     // The next six methods are just getters and setters
     // for the member variables of this class.
     public   Person  getStudent ()   {
         return  student ;
     }

     public   void  setStudent ( Person  student )   {
         this . student  =  student ;
     }

     public   int  getNumericGrade ()   {
         return  numericGrade ;
     }

     public   void  setNumericGrade ( int  numericGrade )   {
         this . numericGrade  =  numericGrade ;
     }

     public   String  getAssessmentName ()   {
         return  assessmentName ;
     }

     public   void  setAssessmentName ( String  assessmentName )   {
         this . assessmentName  =  assessmentName ;
     }

     public   void  printEntry (){
         System . out . println ( student . toString ());
        
         // instantiate a GradeConverter to get the letter grade.
         GradeConverter  converter  =   new   GradeConverter ();
         System . out . println ( "Scored "   +  numericGrade  );
         System . out . println ( "Which is a: "   +  converter . convertGrade ( numericGrade ));
         System . out . println ( "For assessment: "   +  assessmentName );
     }
    
     public   static   void  main ( String []  args )   {
         // instantiate the GradeBookEntry, just like we have
         // done with the Scanner class.
         GradeBookEntry  gradeBookEntry  =   new   GradeBookEntry ();
         Scanner  reader  =   new   Scanner ( System . in );
    
         // instantiate a new person object
         Person  student  =   new   Person ();
        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.java

week3_src/GradeConverter.java

package  edu . drexel . ct290 ;

public   class   GradeConverter   {

     public   String  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
     */
     public   int  getNumberGrade (){
         int  userInput = 0 ;
         // TODO complete this method
         return  userInput ;
     }
     /**
     *  @param  args
     */
     public   static   void  main ( String []  args )   {
         GradeConverter  converter  =   new   GradeConverter ();
         int  input  =  converter . getNumberGrade ();
         String  letterGrade  =  converter . convertGrade ( input );
        
         System . out . println ( "The letter grade for "   +  input  +   " is "   +  letterGrade );
     }

}

__MACOSX/week3_src/._GradeConverter.java

week3_src/GuessTheNumber.java

week3_src/GuessTheNumber.java

package  edu . drexel . ct290 ;

import  java . util . Random ;
import  java . util . Scanner ;

public   class   GuessTheNumber   {
    
     public   int  getRandomNumber (   int  range  ){
         // Instantiate a random number generator
         Random  rand  =   new   Random ();
        
         //Generate a number in the given range
         int  answer  =  rand . nextInt ( range )   +   1 ;
        
         return  answer ;
     }
    
     public   int  getUserGuess (){
         System . out . print ( "Enter your guess: " );
         Scanner  reader  =   new   Scanner ( System . in );
         return  reader . nextInt ();
     }
    
     public   boolean  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 */   ){
             return   true ;
         }
         // TODO: fill in the code to tell if they are too high or too low
        
         return   false ;
     }

     public   static   void  main ( String []  args )   {
         System . out . println ( "Lets play game.  I'll pick a number 1-100 and you guess." );
        
         GuessTheNumber  guessNumber  =   new   GuessTheNumber ();
        
         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.java

week3_src/LogicalOperators.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   LogicalOperators   {

     private   final   int  MALE  =   1 ;
     public   boolean  getInput (   String  question  ){
         Scanner  reader  =   new   Scanner ( System . in );
        
         System . out . println ( question );
         System . out . print ( ">" );
        
         // to enter a boolean you must type in true or false in the console
         return  reader . nextBoolean ();
     }
    
     public   static   void  main ( String []  args )   {
         LogicalOperators  logicalOps  =   new   LogicalOperators ();
        
         boolean  condition1 ;
         boolean  condition2 ;
        
         System . out . println ();
        condition1  =  logicalOps . getInput ( "Is the first condition true 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.java

week3_src/LoopInput.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   LoopInput   {

     public   static   void  main ( String []  args )   {
         Scanner  reader  =   new   Scanner ( 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.java

week3_src/Person.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   Person   {

     private   String  name ;
     private   int  age ;
     private   String  email ;
    
     public   String  getName ()   {
         return  name ;
     }

     public   void  setName ( String  name )   {
         this . name  =  name ;
     }

     public   int  getAge ()   {
         return  age ;
     }

     public   void  setAge ( int  age )   {
         this . age  =  age ;
     }

     public   String  getEmail ()   {
         return  email ;
     }

     public   void  setEmail ( String  email )   {
         this . email  =  email ;
     }
    
     public   void  getPersonData (){
         Scanner  reader  =   new   Scanner ( 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 ();
     }
    
     public   String  toString (){
         return   "Name: "   +  name  +   "\nAge: "   +  age  +   "\nemail: "   +  email ;
     }
}

__MACOSX/week3_src/._Person.java

week3_src/WhileLoopFactorial.java

week3_src/WhileLoopFactorial.java

package  edu . drexel . ct290 ;

import  java . util . Scanner ;

public   class   WhileLoopFactorial   {


     /**
     * 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.
     */
     public   long  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 ;
     }
    
     public   static   void  main ( String []  args )   {
         // Get the user input
         Scanner  reader  =   new   Scanner ( System . in );
         System . out . print ( "What number do you want a factorial for: " );
         int  number  =  reader . nextInt ();
        
         // Create a WhileLoopFactorial class
         WhileLoopFactorial  loopFactorial  =   new   WhileLoopFactorial ();
        
         // 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