Chapter 4/Test 2 Programming

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Write a program that uses a loop to ask the user to enter a number at each loop iteration. The loop should iterate 6 times and keep a running total of the numbers entered

Scanner keyboard = new Scanner(System.in); int total = 0; for (int i = 1; i <= 6; i++) System.out.println("Enter a number"); int value = keyboard.nextInt(); { total = total + value; } System.out.println("The total is " + total); } }

Write a program to calculate the sum of series of numbers. 1* 99 + 2* 98 + 3* 97 + ... + 97* 3+ 98 * 2+99* 1 = ?

int total = 0; for (int i = 1; i <= 100; i++) { total = total + i*(100-i); } System.out.println("The total is " + total); } }

Write a program to calculate the sum of the squares of all positive numbers is less than or equal to 100. 1 * 1 + 2 * 2 + 3 * 3 + ... + 100 * 100 = ?

int total = 0; for (int i = 1; i <= 100; i++) { total = total + i*i; } System.out.println("The total is " + total); } }

1. Write a program to calculate the sum of all positive integers that is less than or equal to 100. 1 + 2 + 3 + ... + 100 = ?

int total = 0; for (int i = 1; i <= 100; i++) { total = total + i; } System.out.println("The total is " + total); } }

Write a program to calculate the sum of all positive even numbers is less than or equal to 100. 2 + 4 + 6 +... + 100 = ?

int total = 0; for (int i = 2; i <= 100; i=i+2) { total = total + i; } System.out.println("The total is " + total); } }

Given a string and a non-negative int n, return a larger string that is n copies of the original string. formRepeatingString("Hi", 3) → "HiHiHi" formRepeatingString("Ho", 4) → "HoHoHoHo" formRepeatingString("He", 6) → "HeHeHeHeHeHe

public String formRepeatingString(String s, int n){ String result = ""; for (int i = 1; i <= n; i++) { result = result + s; } return result; }

Given a string and a non-negative int n, return a larger string that is n copies of the first 2 characters of the original string. For example, if the input string is "Hello", the input int is 3, then the method should return "HeHeHe". (Difficulty Level: 3) formRepeatingSubString("river", 2) → "riri"formRepeatingSubString("BEAR", 3) → "BEBEBE"formRepeatingSubString("Mississippi", 5) → "MiMiMiMiMi"

public String formRepeatingSubString(String s, int n){ String result = ""; String str = s.substring(0,2); for (int i = 0; i < n; i++) { result = result + str; } return result; }

Given a string and a non-negative int n, return a larger string that is n copies of the original string if n is less than or equal to 3; if n is greater than 3, the returned larger string is formed by 3 copies of the original string getMutipleCopies("Hi", 1) → "Hi" getMutipleCopies("Ho", 2) → "HoHo" getMutipleCopies("He", 3) → "HeHeHe"

public String getMutipleCopies(String s, int i){ if (i==1) return s; else if (i==2) return s+s; else return s+s+s; }

We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 3 int values, return the string "NoTeen" if none of them is teen; return the string "OneTeen" if only 1 number is a teen; return the string "TwoTeen" if two of them are teen; return the string "ThreeTeen" if all of them are teen. getTeenString(5, 8, 9) → "NoTeen" getTeenString(11, 14, 7) → "OneTeen" getTeenString(13, 15, 6) → "TwoTeen"

public String getTeenString(int a, int b, int c){ boolean isATeen = a>=13 && a <=19; boolean isBTeen = b>=13 && b <=19; boolean isCTeen = c>=13 && c <=19; if(!isATeen && !isBTeen && !isCTeen) return "NoTeen"; else if (isATeen && isBTeen && isCTeen) return "ThreeTeen"; else if ((isATeen && isBTeen && !isCTeen) || (isATeen && !isBTeen && isCTeen) || (!isATeen && isBTeen && isCTeen)) return "TwoTeen"; else return "OneTeen"; }

Given a string, return a new string that is formed by removing all the appearance of the character 'a' from the original string. For example, "cat" -> "ct" (Difficulty Level: 3) removeAs("Hello") → "Hello" removeAs("access") → "ccess" removeAs("balance") → "blnce"

public String removeAs(String s){ String result = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c != 'a') result = result + c; } return result; }

Given a string, return true if the number of times that character 'a' appears anywhere in the string is more than that of character 'b', otherwise, return false. For example, "aab" -> true, and "abb" -> false(Difficulty Level: 3) isMoreAsThanBs("abb") → false isMoreAsThanBs("aab") → true isMoreAsThanBs("ac") → true

public boolean isMoreAsThanBs(String s){ int countA = 0; int countB = 0; for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); if (c == 'a') countA++; } for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); if (c == 'b') countB++; } if (countA > countB) return true; else return false; }

There is a bank account that earns 4.5 percent interest per year. Given the account balance, return the interest amount earned by the account after one year. (Difficulty Level: 1) getInterest(200) → 9.0 getInterest(1000) → 45.0 getInterest(50000) → 2250.0

public double getInterest(double balance){ return balance * 0.045; }

Given an positive number n, return the sum of all positive integers that is less than or equal to n

public int computeSum(int n){ int sum = 0; for (int i = 0; i <= n; i++) { sum = sum + i; } return sum; }

Given an positive number n, return the sum of all positive even numbers that is less than or equal to n. (Difficulty Level: 2) computeSumOfEvenNumbers(3) → 2 computeSumOfEvenNumbers(5) → 6 computeSumOfEvenNumbers(10) → 30

public int computeSumOfEvenNumbers(int n){ int sum = 0; for (int i = 2; i <=n; i+=2) { sum += i; } return sum; }

Given an positive number n, return the sum of the squares of all positive integers that is no more than n. computeSumOfSquares(1) → 1 computeSumOfSquares(2) → 5 computeSumOfSquares(3) → 14

public int computeSumOfSquares(int n){ int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + (i * i); } return sum; }

Given a string, return the number of times that the character "a" appears anywhere in the given string. (Difficulty Level: 3) countAs("Hello") → 0 countAs("access") → 1 countAs("balance") → 2

public int countAs(String s){ int count = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'a') count++; } return count; }

There is a bank account that earns 5 percent interest per year. The initial account balance is $10000. Given a target balance (for example, $20000), return the number of years that it takes for the account balance to reach the target balance. The number of years should be an integer. (Difficulty level: 2) getNumberOfYears(12000) → 4 getNumberOfYears(15000) → 9 getNumberOfYears(20000) → 15

public int getNumberOfYears(double targetBalance){ int years = 0; double balance = 10000; while (balance < targetBalance) { double interest = balance * 0.05; balance = balance + interest; years++; } return years; }

Write a program that uses a for loop to display the following set of numbers: 100, 99, 98, 97, ..., 0

public static void main(String[] args) { for (int i = 100; i >= 0; i--) { System.out.print(i + ","); } } }

Write a program that uses a forloop to display the following set of numbers:50, 51,52, 53, 54, ..., 100

public static void main(String[] args) { for (int i = 50; i <= 100; i++) { System.out.print(i + ","); } } }

Write a program that uses a while loop to display the following set of numbers: 0, 1, 2, 3, ..., 100

public static void main(String[] args) { int i = 0; while (i <= 100) { System.out.print(i+", "); i++; } } }

Write a program that uses a while loop to display the following set of numbers: following set of numbers: 0, 10, 20, 30, ..., 1000

public static void main(String[] args) { int i = 0; while (i <= 1000) { System.out.print(i+", "); i = i + 10; } } }

Write a program that contains a while loop which is used to display 10 rows of strings "I love Java!"

public static void main(String[] args) { int i = 10; while (i >= 1) { System.out.println("I love Java!"); i--; } } }

Write a program that contains a while loop that displays the following messages: 9 weeks left before Thanksgiving! 8 weeks left before Thanksgiving! 7 weeks left before Thanksgiving! 6 weeks left before Thanksgiving! 5 weeks left before Thanksgiving! 4 weeks left before Thanksgiving! 3 weeks left before Thanksgiving! 2 weeks left before Thanksgiving! 1 weeks left before Thanksgiving! Happy Thanksgiving!

public static void main(String[] args) { int i = 9; while (i > 0) { System.out.println(i + " weeks left to Thanksgiving"); --i; } System.out.println("Happy Thanksgiving!"); } }


संबंधित स्टडी सेट्स

Chapter 69: Arthritis and Connective Tissue Diseases

View Set

Christianity, Judaism, and Islam

View Set

anthropology 2019 concept questions

View Set

CSI Practice Guide Glossary of Terms #2

View Set

Chapter 7 B Law (Negligence and Strict Liability)

View Set

Introduction to Central Nervous System Pharmacology

View Set

ch 22 NCLEX questions, Chapter 22, Chapter 22: Neurodevelopmental Disorders, Psych CH 22

View Set

Texas Principles of Real Estate 2 - Chp. 2 Real Estate Appraisal

View Set

BJU Earth Science Fourth Edition Chapter 22 Review

View Set

Queen Elizabeth 1 - interesting and useful facts

View Set

Ch.15 Homework Personal Safety and Injury Prevention

View Set