Package exercises.chapter1

Source Code of exercises.chapter1.Reinforcement

package exercises.chapter1;


import java.util.Scanner;


import exercises.manualTests.TestCreditCard;
import exercises.models.CreditCard;
import exercises.models.Flower;
import exercises.models.GameEntry;
import exercises.utils.MathHelpers;


public class Reinforcement {
 
  private CreditCard card = new CreditCard("3456 6543 7889 6432", "Fictional Guy", "CIBC", 4000.0, 5000, 0.125);
 
  public void r_1_1() { 
   
    int accessor = 4;
   
    GameEntry[] gameEntriesA = { new GameEntry( 0 ), new GameEntry( 1 ), new GameEntry( 2 ), new GameEntry( 3 ), new GameEntry( 4 ), new GameEntry( 5 ), new GameEntry( 6 ) };
   
    GameEntry[] gameEntriesB= gameEntriesA;
    gameEntriesB[ accessor ].setScore( 550 );
   
    System.out.println( "Q: If we then immediately set A [4].score equal to 550, what is the score value of the GameEntry object referenced by B[4]?" );
    System.out.println( "A:" + gameEntriesA[ 4 ].getScore() );
   
  }
 
  public void r_1_2() {
   
    System.out.println( "Q: Modify the CreditCard class from Code Fragment 1.5 to charge interest on each payment." );
    System.out.print( "A: " );
    CreditCard.printCard( card );
    card.makePayment( 20 );
   
  }
 
  public void r_1_3() {
   
    System.out.println( "Q: Modify the CreditCard class from Code Fragment 1.5 to charge a late fee for any payment that is past its due date." );
    System.out.println( "A: " );
    CreditCard.printCard( card );
    card.setPaymentDueDay( 0 );
    card.makePayment( 20 );
   
  }
 
  public void r_1_4() {
   
    System.out.println( "Q: Modify the CreditCard class from Code Fragment 1.5 to include modifier methods, which allow a user to modify internal variables in a CreditCard class in a controlled manner." );
    System.out.println( "A: " );
   
    CreditCard.printCard( card );
    card.setInterestRate( 0.29 );
    card.setLimit( 8000 );
    card.setPaymentDueDay( 20 );
    System.out.println( "\nValues changed via setter" );
    CreditCard.printCard( card );
  }
 
  public void r_1_5() {
   
    System.out.println( "Q: Modify the declaration of the first for loop in the Test class in Code Fragment 1.6 so that its charges will eventually cause exactly one of the three credit cards to go over its credit limit." );
    System.out.println( "A: " );
   
    new TestCreditCard().Run();
   
  }
 
  public void r_1_6() {
   
    System.out.println( "Q: Write a short Java function, inputAllBaseTypes, that inputs a different value of each base type from the standard input device and prints it back to the standard output device." );
    System.out.println( "A: " );
   
    Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      while( !input.hasNextInt() ) {
        input.nextLine();
        System.out.print("Not an integer, try again: ");
      }
      System.out.println( "You entered: " + input.nextInt() );
     
      System.out.print("Enter a BigDecimal: ");
      while( !input.hasNextBigDecimal() ) {
        input.nextLine();
        System.out.print("Not a BigDecimal, try again: ");
      }
      System.out.println( "You entered: " + input.nextBigDecimal() );
     
      System.out.println( "Enter a long: " );
      while( !input.hasNextLong() ) {
        input.nextLine();
        System.out.println( "Not a long, try again" );
      }
      System.out.println( "You entered: " + input.nextLong() );
     
      System.out.println( "Enter a double: " );
      while( !input.hasNextDouble() ) {
        input.nextLine();
        System.out.println( "Not a double, try again" );
      }
      System.out.println( "You entered: " + input.nextDouble() );
     
      System.out.println( "Enter a boolean: " );
      while( !input.hasNextBoolean() ) {
        input.nextLine();
        System.out.println( "Not a boolean, try again" );
      }
      System.out.println( "You entered: " + input.nextBoolean() );
     
      System.out.println( "Enter a BigInteger: " );
      while( !input.hasNextBigInteger() ) {
        input.nextLine();
        System.out.println( "Not a BigInteger, try again" );
      }
      System.out.println( "You entered: " + input.nextBigInteger() );
     
      System.out.println( "Enter a Byte: " );
      while( !input.hasNextByte() ) {
        input.nextLine();
        System.out.println( "Not a Byte, try again" );
      }
      System.out.println( "You entered: " + input.nextByte() );
     
      System.out.println( "Enter a Float: " );
      while( !input.hasNextFloat() ) {
        input.nextLine();
        System.out.println( "Not a Float, try again" );
      }
      System.out.println( "You entered: " + input.nextFloat() );
  }
 
  public void r_1_7() {
   
    System.out.println( "Q: Write a Java class, Flower, that has three instance variables of type String, int, and float, which respectively represent the name of the flower, its number of pedals, and price." );
    System.out.println( "A: " );
   
    Flower flower = new Flower( "Petunia", 5, 1.35 );
    System.out.println( flower );
   
    System.out.println( "\nUsing setters to alter values" );
    flower.setName( "Orchid" );
    flower.setNumberOfPetals( 20 );
    flower.setPrice( 50 );
    System.out.println( flower );
   
  }
 
  public void r_1_8() {
   
    System.out.println( "Q: Write a short Java function, isMultiple, that takes two long values, n and m, and returns true if and only if n is a multiple of m" );
    System.out.println( "A: " );
   
    System.out.println( "Is 5 a multiple of 10?: " + MathHelpers.isMultiple( 5, 10 ) );
    System.out.println( "Is 10 a multiple of 5?: " + MathHelpers.isMultiple( 10, 5 ) );
    System.out.println( "Is 3 a multiple of 23?: " + MathHelpers.isMultiple( 3, 23 ) );
    System.out.println( "Is 3 a multiple of 9?: " + MathHelpers.isMultiple( 3, 9 ) );
   
  }
 
  public void r_1_9() {
   
    System.out.println( "Q: Write a short Java function, isOdd, that takes an int i and returns true if and only if i is odd." );
    System.out.println( "A: " );
   
    System.out.println( "Is 0 Odd?: " + MathHelpers.isOdd( 0 ) );
    System.out.println( "Is 1 Odd?: " + MathHelpers.isOdd( 1 ) );
    System.out.println( "Is 2 Odd?: " + MathHelpers.isOdd( 2 ) );
    System.out.println( "Is 23453 Odd?: " + MathHelpers.isOdd( 23453 ) );
   
  }
 
  public void r_1_10() {
   
    System.out.println( "Q: Write a short Java function that takes an integer n and returns the sum of all the integers smaller than n." );
    System.out.println( "A: " );
   
    System.out.println( "Series from 0 with gauss: " + MathHelpers.seriesSumWithGaussFormula( 0 ) );
    System.out.println( "Series from 0 with loop: " + MathHelpers.seriesSumWithLoop( 0 ) );
   
    System.out.println( "Series from 2 with gauss: " + MathHelpers.seriesSumWithGaussFormula( 2 ) );
    System.out.println( "Series from 2 with loop: " + MathHelpers.seriesSumWithLoop( 2 ) );
   
    System.out.println( "Series from 20 with gauss: " + MathHelpers.seriesSumWithGaussFormula( 20 ) );
    System.out.println( "Series from 20 with loop: " + MathHelpers.seriesSumWithLoop( 20 ) );
   
    System.out.println( "Series from 100 with gauss: " + MathHelpers.seriesSumWithGaussFormula( 100 ) );
    System.out.println( "Series from 100 with loop: " + MathHelpers.seriesSumWithLoop( 100 ) );
   
    System.out.println( "Series from -5 with gauss: " + MathHelpers.seriesSumWithGaussFormula( -5 ) );
    System.out.println( "Series from -5 with loop: " + MathHelpers.seriesSumWithLoop( -5 ) );
   
  }
 
  public void r_1_11() {
   
    System.out.println( "Q: Write a short Java function that takes an integer n and returns the sum of all the odd integers smaller than n." );
    System.out.println( "A: " );
   
    System.out.println( "Odd Series from 0: " + MathHelpers.seriesSumOfOddNumbers( 0 ) );
    System.out.println( "Should be: " + 0 );
   
    System.out.println( "Odd Series from 13: " + MathHelpers.seriesSumOfOddNumbers( 13 ) );
    System.out.println( "Should be: " + ( 11 + 9 + 7 + 5 + 3 + 1 ) );
   
    System.out.println( "Odd Series from -13: " + MathHelpers.seriesSumOfOddNumbers( -13 ) );
    System.out.println( "Should be: " + 0 );
   
    System.out.println( "Odd Series from 16: " + MathHelpers.seriesSumOfOddNumbers( 16 ) );
    System.out.println( "Should be: " + ( 15 + 13 + 11 + 9 + 7 + 5 + 3 + 1 ) );
  }

}
TOP

Related Classes of exercises.chapter1.Reinforcement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.