Assignments
In the following exercises, use the bold terms as variable names.
- Assign 43 to a variable called age.
- Assign 18.25 to a variable called cost.
- Given a rectangle with sides of length width and height, calculate the area.
- Given a container with dimensions of width, length and height, calculate the area.
- Given three integers called one, two and three, calculate the average.
- Given a circle with a radius, calculate the circumference using Math.PI.
- Given a circle with a diameter, calculate the area using Math.PI.
- Given a speed (in miles per hour) and a time (in hours), calculate the distance traveled.
- Calculate your height in inches given your height in feet and inches.
- Calculate the square of given integer num.
- Calculate the wage, given a payrate and number of hours worked.
- Calculate the cube of given integer num.
- One acre of land is 43,560 square feet. Calculate the number of acres is a parcel of land with a certain squareFeet.
- Calculate the miles per gallon (mpg) of a vehicle, given the miles driven and the gallons of gas used.
- Calculate the profits if a company earns 40% on all sales.
- Calculate the distance between two Points p1 and p2. Points are objects that have getX() and getY() methods. You may use the Math.sqrt(num) method.
- Convert a weight in pounds to kilograms (kg).
- Convert a distance in miles to kilometers (km).
- Calculate the distance between two points represented by (x1, y1) and (x2, y2).
- Generate a random number between 1 and 6.
- Generate a random number between min and max (inclusive) even if the numbers are negative.
- Given an amount of cents between 1 and 100, determine the minimum number of coins needed to represent the amount. For example, 75 cents can be provided in 3 quarters and 41 cents can be 1 quarter, 1 dime, 1 nickel and 1 penny.
- Convert a temperature given in Fahrenheit to celsius. Track down the conversion formula if not already known.
- Calculate roundedDown given an integer num. Round down to the nearest tens position. For example, 37 becomes 30 and 212 becomes 210.
- int result = 3 / 4;
- double result = 3 / 4;
- int result = 10 % 4;
- int result = 10 / 4;
- int result = 5 + 2 * 4;
- int result = 6 - 3 * 2 + 7 - 1;
- Write your own question(s) to test the use of assignment statements.
Conditional Statements
In the following exercises, use the bold terms as variable names.
- Given two integers one and two, identify the largest value.
- Given three integers one, two and three, identify the largest value.
- Calculate the absolute value of an integer num.
- Given water at a specific temp, print if the water is ice, steam or liquid.
- Calculate a salesperson's salary if she earns a 10% commission on sales and a $500 bonus if sales are over $10,000.
- Increase payRate by 50% if hours worked is greater than forty.
- Given a number, print if it is positive or negative.
- Given a number, print if it is even or odd.
- Given a numerical score (1-100), print the corresponding letter grade of A, B, C, D or F.
- Write your own question(s) to test the use of conditional statements.
- Given a number of seconds, print the number of days, hours and minutes.
- Given a number, print a message if the value is between 100 and 200.
- Given a number (1-10), print the corresponding roman numeral.
- Determine if integer month is a valid calendar month (1-12). Set boolean isValid to true if it is.
- Given an integer year, determine if it is a leap year. Set boolean leapYear to true if it is. Track down the rules for a leap year if not already known.
- Given a day of the year (1-365), print the month and day. For example, 45 would be February 14.
- Write your own question(s) to test the use of conditional statements.
Loops
In the following exercises, use the bold terms as variable names. Unless specified choose the most appropriate loop structure.
- Given a positive integer n, calculate the sum of all integers from 1 to n.
- Given two integers min and max, calculate the sum of all values between min and max (inclusive)
- Given an integer n, calculate the factorial of n. As an example, recall that the factorial of 4 = 4*3*2*1.
- Calculate result given positive integers base and exponent. Base is raised to the power of exponent. For example, a base of 4 and exponent of 3 results in 64.
- Print your name n times.
- Given a String, count the number of vowels (a, e, i, o, u).
- Given an integer n, reverse the digits to create a new number. For example, 123 becomes 321 and 4236 becomes 6324.
- Sum the values 1/30 + 2/29 + 3/28 + 4/27 .... + 30/1.
- Given an integer value, determine the number of odd, even and zero digits. For example, 230460 has 2 zeros, 3 evens and 1 odd.
- Print a provided String one character per line.
- Print the first 100 multiples of value. For example, if value = 3 then print 3, 6, 9, 12 ... 300.
- Given a String of digits, convert the value to an integer without using the built-in Integer.parseInt( ) method.
- Given an investment starting amount, calculate the final amount after N years that increases by a certain rate each year. The rate is provided as a decimal (i.e. 0.1 would be a 10% increase per year).
- Determine how many months it will take to pay off a loan given the starting balance, monthly payment, and yearly interest rate. The rate is provided as a decimal (i.e. 0.1 would be a 10% interest per year). The rate must be converted to monthly and applied to the balance each month before the payment is subtracted.
- Consider the following simple game where you keep rolling a die in order to get a total of 8 from the last two
rolls and record the number of rolls needed to get the total. The following are examples from two sessions of
the game:
3 3 1 6 2
You win after 5 rolls
1 6 3 4 6 1 2 5 3
You win after 9 rolls
- Print the lyrics to "Twelve Days of Christmas". Use a switch statement to print the correct line for each day.
- Print the lyrics to "One Hundred Bottles of Beer on the Wall".
- Write your own question(s) to test the use of loops.
Nested Loops
- Given two integers width and height, print a filled rectangle using asterisk.
- Given two integers width and height, print only the outline of a rectangle using asterisks.
- Given an integer called size, print an X
- Starting at 5 hours , print a count down to zero: 5:00, 4:59, 4:58, 4:57 ... 0:03, 0:02, 0:01, 0:00
- print the multiplication table from 1 to 10. Use the tab character for an attractive layout.
Methods
- double calcEstimate(int sqFt) - A gallon of paint covers 115 square feet and four hours of labor to apply. XYZ Painters charge $18 per gallon and $22 per hour. Write a method that is passed the square footage of a job and returns the estimated cost.
- boolean isPrime(int value) - write a method that returns true if the passed value is a prime number.
- double abs(double val) - returns the absolute value.
- int calcAmount(int days) - w rite a method that is passed an integer days that returns the amount of money you would have if you started with one penny and it doubled every day.
- void printFactors(int n) - print all factors of n. For example, the factors of 8 are 1, 2, 4, 8. The factors of 9 are 1, 3, 9. The factors of 11 are 1, 11.
- int factorial(int N) - write a method to calculate the factorial of N. Recall that the factorial of 5 is 5 x 4 x 3 x 2 x 1.
- double pizzaOrder(int people) - Calculate cost of a pizza party given a number of people who will attend. People eat 2.5 pieces on average and a large pizza has eight slices. A large pizza costs $14 and there is a 10% delivery charge.
- boolean isPalindrone(String str) - return true if the String is identical forward and backward. For example, "bob", "kayak", and "able was I ere I saw elba".
- int rollDice () - return the number of die rolls it takes until two consecutive rolls total exactly eight.
For example, the sequence 3, 3, 1, 6, 2 takes five rolls but the sequence 5, 3 takes only two rolls.
- int reverseIt(int num) - return a new integer that contains the reversed digits. For example, 2536 becomes 6352.
- Write your own question(s) to test the use of methods.
Arrays
A. Given an array of random integers called nums:
- identify the smallest element without sorting
- identify the largest element without sorting
- calculate the sum of all elements
- calculate the average of all elements which should be of type double
- square each element
- print all elements (10 per line)
- print all elements from last to first
- print all elements from the middle out assuming an odd number of elements. For example, an array containing [1, 2, 3, 4, 5] would print 3, 2, 4, 1, 5.
- calculate the sum of all odd numbers in the array. For example, if there was nothing but even numbers then the sum would be zero.
- calculate the longest sequence of increasing values. For example, 1 3 7 10 is a sequence of increasing values but 3 7 1 and 4 1 7 3 is not.
- identify the most commonly occurring element (called the mode). Assume the elements are sorted.
- reverse the contents of the array. For example, {1, 2, 3, 4, 5} becomes {5, 4, 3, 2, 1} but the values are not necessarily sorted.
- Given an array of doubles representing stock prices for each of the last N days, calculate the probability of the stock value rising tomorrow.
- Copy the contents of array1 to array2.
- Simulate a lottery drawing. Six ping pong balls are randomly selected from values of 1 - 45 with no duplicates.
- Write your own question(s) to test the use of arrays.
Classes
Write the following classes. Be sure to test each method.
- Coin - write a class to simulate a coin that can be tossed and results in heads or tails. Use the Random class as an instance variable. Include methods to flip() the coin and to determine if the coin isHeads().
- Dice - write a class to simulate a six-sided die. Use the Random class as an instance variable. Include methods to roll() the die and to getValue().
- Card - write a class to simulate a playing card. Maintain information about the suit (club, diamond, spade, heart) and the value (ace through king).
Terms and Concepts
You should be able to describe the following terms and concepts in two or three carefully written sentences.
- define the state and behavior of an object
- describe the differences between a class and an object
- compare the differences between local variables an instance variables with respect to their scope and lifetime
- explain the difference between the '=' and '==' operators
- describe the difference between static and instance members
- compare the differences between public and private members
- describe the software development goal of high cohesion
- describe the software development goal of loose coupling
|