Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Chapter 6 Exercises
- 1. while: A loop structure, which executes a set of statements over and over again based on a condition.
- do-while: In the do-while statement the condition is not evaluated until after the first execution of the loop.
- 2.
- package review.prompter;
- import java.util.Scanner;
- public class ReviewPrompter {
- public static void main(String[] args) {
- int min, max, num;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter the minimum number: ");
- min = input.nextInt();
- System.out.print("Enter the maximum number: ");
- max = input.nextInt();
- System.out.print("Enter a number between " + min +" and " + max +": ");
- num = input.nextInt();
- while(num >= max || num <= min){
- System.out.print("Wrong! Try again: ");
- num = input.nextInt();
- }
- }
- }
- 3. A Sentinel is a constant that stores a value that is used to signify that a loop should stop iterating.
- 4.
- package review_percentpassing;
- import java.util.Scanner;
- public class Review_PercentPassing {
- public static void main(String[] args) {
- int score;
- int scoreValues = 0;
- int passing = 0;
- double avg;
- final int SENTINEL = 0;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a score out of 100 (Enter 0 to finish): ");
- score = input.nextInt();
- while(score != SENTINEL){
- scoreValues += 1;
- if(score >= 70){
- passing += 1;}
- System.out.print("Enter another score: ");
- score = input.nextInt();}
- input.close();
- avg=(double)passing / (double)scoreValues;
- System.out.printf("%2.2f %2s\n",avg * 100 , " Percent Passing");
- }
- }
- 5.
- package review_oddsum;
- import java.util.Scanner;
- public class Review_OddSum {
- public static void main(String[] args) {
- int num;
- int num2 = 1;
- int oddSum = 0;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a number: ");
- num = input.nextInt();
- while (num2 != num){
- num2 ++;
- if (num2 % 2 == 1){
- oddSum ++;
- }
- }
- System.out.print("Odd sum is : " + oddSum);
- }
- }
- 6.
- package chapter6.pkg6;
- import java.util.Scanner;
- public class Chapter66 {
- public static void main(String[] args) throws Exception {
- for ( int i=0; i<80; i++ ){
- if ( i%10 == 0 ){
- System.out.print("Parkside Robotics is cool. \t");
- Thread.sleep(200);}
- }
- }
- }
- 7.
- package stringdemo;
- import java.util.Scanner;
- public class StringDemo {
- public static void main(String[] args) {
- String phrase, threeLetters;
- int phraseLength;
- int mid;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter text that contains at least three characters: ");
- phrase = input.nextLine();
- input.close();
- phraseLength = phrase.length();
- mid = phraseLength / 2;
- threeLetters = phrase.substring(mid - 1, mid + 2);
- System.out.println("Middle three characters are: "
- + threeLetters);
- }
- }
- 8. a) The position of a character in string is called its index
- b) A string is said to be immutable because it cannot be changed
- c) A string without an assigned value
- d) Generated from calling a methof from a String object
- e) The Unicode Standard is a 16-bit encoding system that assigns a value for each character and symbol of every language
- 9.
- package review_accountsetup;
- import java.util.Scanner;
- public class Review_AccountSetup {
- public static void main(String[] args) {
- String username, password;
- int passLength;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a username: ");
- username = input.nextLine();
- System.out.print("Enter a password that is at least 8 characters: ");
- password = input.nextLine();
- passLength = password.length();
- while (passLength < 8){
- System.out.print("Enter a password that is at least 8 characters: ");
- password = input.nextLine();
- passLength = password.length();}
- username = username.toLowerCase();
- password = password.toLowerCase();
- System.out.println("Your username is "+ username + " and your password is " + password);
- }
- }
- 10.
- package formalgreeting;
- import java.util.Scanner;
- public class FormalGreeting {
- public static void main(String[] args) {
- String name, nameL;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter your name, including your title: ");
- name = input.next();
- nameL = name.toLowerCase();
- if ("mr.".equals(nameL) || "mr".equals(nameL)){
- System.out.println("Hello, sir.");
- }else{
- if ("ms.".equals(nameL) || "ms".equals(nameL) || "mrs.".equals(nameL) || "mrs.".equals(nameL) || "miss".equals(nameL)){
- System.out.println("Hello, ma'am.");
- }else{
- System.out.println("Hello, " + name);}}
- }
- }
- 11.
- #5 60 times
- #6 x = 0
- #11 a) 10
- b) "my "
- c) "my string."
- d) There would be an error that would be fixed by adding () after "toUpperCase"
- e) "my string."
- #12 a) false
- b) true
- c) 24
- d) 0
- e) -8
- f) 9
- g) 9
- h) true
- i) true
- 12.
- #18 Monogram
- package monogram;
- import java.util.Scanner;
- public class Monogram {
- public static void main(String[] args) {
- String first, middle, last;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter yuor first name: ");
- first = input.nextLine();
- first = first.toLowerCase();
- System.out.print("Enter your middle initial: ");
- middle = input.nextLine();
- middle = middle.toLowerCase();
- System.out.print("Enter your last name: ");
- last = input.nextLine();
- last = last.toUpperCase();
- input.close();
- System.out.println("Your monogram is: " + first.substring(0,1) + last.substring(0,1) + middle.substring(0,1));
- }
- }
- #19 RemoveString
- package removestring;
- import java.util.Scanner;
- public class RemoveString {
- public static void main(String[] args) {
- String sentence, remove, sentence2;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a sentence: ");
- sentence = input.nextLine();
- System.out.print("Enter a string: ");
- remove = input.nextLine();
- input.close();
- sentence2 = sentence.replace(remove + "","");
- System.out.println(sentence2);
- }
- }
- #21 GroupAssignment
- package groupassignment;
- import java.util.Scanner;
- public class GroupAssignment {
- public static void main(String[] args) {
- String first, last;
- int group1, group2;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter your first name: ");
- first = input.nextLine();
- System.out.print("Enter your last name: ");
- last = input.nextLine();
- input.close();
- group1 = last.compareToIgnoreCase("i");
- if (group1 < 1){
- System.out.println(first + " " + last + " is assigned to Group 1");}
- else{
- group2 = last.compareToIgnoreCase("s");
- if (group2 < 1){
- System.out.println(first + " " + last + " is assigned to Group 2");}
- else{
- System.out.println(first + " " + last + " is assigned to Group 3");}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement