Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Daniel Lawson Chapter 5 test
- //Question 8
- //-------------------------------------------------------------------------------------------------------------------------------------
- /**
- * Created by Daniel on 5/15/2016.
- */
- import java.util.*; //import for scanner and random use
- public class PrintTwoDigit {
- public static void main(String[] args){
- Scanner s = new Scanner(System.in);//create scanner for user input
- Random r = new Random();//create random to pass to the method
- //explain the program to the user
- 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");
- System.out.print("Enter a number between 10 and 99: ");//prompt the user for the number to look for
- int l = s.nextInt();//assign the number to an integer
- while(l<10||l>99){//ask them until they enter a valid input
- System.out.print("Enter a number between 10 and 99: ");
- l = s.nextInt();
- }
- System.out.print("How many times do you want to look for it?");// ask for the number of times to look for the number
- int n = s.nextInt();//assign to int n
- while(n<=0){//ask until the user inputs a valid number (not less than or equal to 0)
- System.out.println("You can not look for something 0 or less times!,enter a new int: ");
- n = s.nextInt();
- }
- printTwoDigit(r,n,l);//Call the printTwoDigit with passing values to that of the user input
- }
- 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
- boolean hasNumber = false;//holds truth value of whether or not integer l has been found
- int num = 0; // holds value of a randomly found number
- for(int i = 1; i<= n;i++){//for loop that gets number and compares them to int l, n times
- num = r.nextInt(90) + 10;//get a random integer in the range of 10-99
- System.out.printf("Next = %d\n",num);//print the found integer
- if(num == l)hasNumber = true;//make hasNumber true if the temporary number is equal to the desired l
- }// at the end of the for loop print to the user whether the number was seen
- if(hasNumber)System.out.printf("we saw a %d\n",l);//tell them the number was seen if it was
- else System.out.printf("no %d was seen\n",l);//tell them not if otherwise
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Question 7
- /**
- * Created by Daniel on 5/16/2016.
- */
- import java.util.*;//import java.util.* for user input
- public class canMakeChange {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);//create a scanner
- System.out.println("We will tell you if we can make change using pennies and nickels");//explain the program to th euser
- System.out.print("Enter how many cents you have: ");//ask how many cents they have
- int c = s.nextInt();//store the value they enter
- System.out.print("Enter how many pennies you have: ");//ask how many pennies they have
- int p = s.nextInt();//store the value
- System.out.print("Enter how many nickles you have: ");//ask how many nickles
- int n = s.nextInt();//store the value
- 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
- else System.out.println("We can not make change");//if not tell them we can not
- }
- public static boolean canMakeChange(int p, int n, int c) {//create method with parameters p for pennies, n for nickles, and c for cents
- 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
- for (int i = 1; i <= p; i++) c -= 1;//subtract a penny for each penny, its ok of the value becomes negative
- return (c <= 0);//return the truth value of the end count being <=0
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------// Question 6
- /**
- * Created by Daniel on 5/12/2016.
- */
- public class consecutiveNumbers {
- public static void main(String[] args) {
- c(8392777);
- }
- public static void c(int number) {//4556
- int prevN;//stores previous number
- int n;//stores current number
- int tempCount = 1;//temporary count of consecutive
- int count = 1; // hold max number of consecutive
- prevN = number % 10;//Make first number a "previous number" so that the first "number" in the method will have
- // something to call to
- number /= 10;//reduce size of imputed number
- while (number > 0) {//run until there is nothing of the number left
- n = number % 10;//get next number
- if (n == prevN) {// if the new number is equal to the last one
- tempCount++;//up the temp count
- }
- else{
- tempCount = 1;//reset the temp count to 1
- }
- count = Math.max(tempCount, count);// see if the new temp count is bigger than the previous max count
- number/=10;//reduce size of number
- prevN = n;// make previous number equal to what was the current number
- }//*2 or //2 + 1 or -1
- System.out.printf("There are %d consecutive numbers",count);// print the results
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment