daniel_lawson9999

Daniel Lawson Test Chapter 5

May 16th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. //Daniel Lawson Chapter 5 test
  2. //Question 8
  3. //-------------------------------------------------------------------------------------------------------------------------------------
  4. /**
  5.  * Created by Daniel on 5/15/2016.
  6.  */
  7. import java.util.*; //import for scanner and random use
  8. public class PrintTwoDigit {
  9.     public static void main(String[] args){
  10.         Scanner s = new Scanner(System.in);//create scanner for user input
  11.         Random r = new Random();//create random to pass to the method
  12.         //explain the program to the user
  13.         System.out.println("In this program we will ask you for a number in the range of 10-99,\nand how may times to look for it,\nwe will then tell you if we found it or not\n");
  14.         System.out.print("Enter a number between 10 and 99: ");//prompt the user for the number to look for
  15.         int l = s.nextInt();//assign the number to an integer
  16.         while(l<10||l>99){//ask them until they enter a valid input
  17.             System.out.print("Enter a number between 10 and 99: ");
  18.             l = s.nextInt();
  19.         }
  20.         System.out.print("How many times do you want to look for it?");// ask for the number of times to look for the number
  21.         int n = s.nextInt();//assign to int n
  22.         while(n<=0){//ask until the user inputs a valid number (not less than or equal to 0)
  23.             System.out.println("You can not look for something 0 or less times!,enter a new int: ");
  24.             n = s.nextInt();
  25.         }
  26.         printTwoDigit(r,n,l);//Call the printTwoDigit with passing values to that of the user input
  27.     }
  28.  
  29.  
  30.     public static void printTwoDigit(Random r, int n,int l){// create method with parameters for the random, n how many times to look for the number, and l the number to look for
  31.         boolean hasNumber = false;//holds truth value of whether or not integer l has been found
  32.         int num = 0; // holds value of a randomly found number
  33.         for(int i = 1; i<= n;i++){//for loop that gets number and compares them to int l, n times
  34.             num = r.nextInt(90) + 10;//get a random integer in the range of 10-99
  35.             System.out.printf("Next = %d\n",num);//print the found integer
  36.             if(num == l)hasNumber = true;//make hasNumber true if the temporary number is equal to the desired l
  37.         }// at the end of the for loop print to the user whether the number was seen
  38.         if(hasNumber)System.out.printf("we saw a %d\n",l);//tell them the number was seen if it was
  39.         else System.out.printf("no %d was seen\n",l);//tell them not if otherwise
  40.  
  41.     }
  42. }
  43. //-------------------------------------------------------------------------------------------------------------------------------------
  44. // Question 7
  45. /**
  46.  * Created by Daniel on 5/16/2016.
  47.  */
  48. import java.util.*;//import java.util.* for user input
  49. public class canMakeChange {
  50.     public static void main(String[] args) {
  51.         Scanner s = new Scanner(System.in);//create a scanner
  52.         System.out.println("We will tell you if we can make change using pennies and nickels");//explain the program to th euser
  53.         System.out.print("Enter how many cents you have: ");//ask how many cents they have
  54.         int c = s.nextInt();//store the value they enter
  55.         System.out.print("Enter how many pennies you have: ");//ask how many pennies they have
  56.         int p = s.nextInt();//store the value
  57.         System.out.print("Enter how many nickles you have: ");//ask how many nickles
  58.         int n = s.nextInt();//store the value
  59.         if(canMakeChange(p,n,c))System.out.println("We can make change");//call the method with the values they entered, tell them if they can make change
  60.         else System.out.println("We can not make change");//if not tell them we can not
  61.     }
  62.  
  63.     public static boolean canMakeChange(int p, int n, int c) {//create method with parameters p for pennies, n for nickles, and c for cents
  64.         for (int i = 1; i <= n; i++) if (c - 5 >= 0) c -= 5;//run a for loop for each nickle, if a nickle can be subtracted without making the value negaitve then subtract
  65.         for (int i = 1; i <= p; i++) c -= 1;//subtract a penny for each penny, its ok of the value becomes negative
  66.         return (c <= 0);//return the truth value of the end count being <=0
  67.     }
  68. }
  69. //-----------------------------------------------------------------------------------------------------------------------------------// Question 6
  70. /**
  71.  * Created by Daniel on 5/12/2016.
  72.  */
  73. public class consecutiveNumbers {
  74.     public static void main(String[] args) {
  75.         c(8392777);
  76.     }
  77.     public static void c(int number) {//4556
  78.         int prevN;//stores previous number
  79.         int n;//stores current number
  80.         int tempCount = 1;//temporary count of consecutive
  81.         int count = 1; // hold max number of consecutive
  82.         prevN = number % 10;//Make first number a "previous number" so that the first "number" in the method will have
  83.         // something to call to
  84.         number /= 10;//reduce size of imputed number
  85.         while (number > 0) {//run until there is nothing of the number left
  86.             n = number % 10;//get next number
  87.             if (n == prevN) {// if the new number is equal to the last one
  88.                 tempCount++;//up the temp count
  89.             }
  90.             else{
  91.                 tempCount = 1;//reset the temp count to 1
  92.             }
  93.             count = Math.max(tempCount, count);// see if the new temp count is bigger than the previous max count
  94.             number/=10;//reduce size of number
  95.             prevN = n;// make previous number equal to what was the current number
  96.         }//*2 or //2 + 1 or -1
  97.         System.out.printf("There are %d consecutive numbers",count);// print the results
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment