The area of a triangle is calculated using the following function:
Area = (h*b)/2
where Area represents the area, h represents the height perpendicular to the length of its base, b.
if h = 6 and b = 4, (6 * 4) is 24 24 / 2 is 12 = 12
What is Area, if h = 4 and b = 5 ? What is Area, if h = 2 and b = 6 ?
What do you need to do to this function to make it use the triangle equation?
public static double triangleArea(double h, double b) {
double area = 0;
return area;
}
System.out.println(triangleArea(4,5));
System.out.println(triangleArea(2,6));If asked to write a function to calculate and return a number divided by 2, you would code
public static double f(double x) {
return x / 2;
}
x is the parameter, the number we want to divide by 2. We just return x / 2.
So if x=6, the number returned by the function will be 3.
Use these as patterns to solve the problems below.
Complete the function to calculate multiplication of two input values. 4 x 4 should print 16.
public static int multiply(int x, int y) {
return 0;
}
Write a simple function that returns the remainder (modulus) of the following equation: 118 % 31
public static int foo() {
return 0;
}
Using Arithmetic Operators, write a function to return the product of 789 x 123.
public static int m() {
return 0;
}
Complete the function to calculate the square of an input value. Remember that the square of a number is the result of multiplying that number times itself.
public static int square(int y) {
return 0;
}
Variables, as you will remember, allow a program to "name" various data and keep track of it as it changes during the running of a program. You can imagine that a computer game which tracks the scores of each player, would keep these values in variables as the game is played.
Write a function one which creates a variable called t with the value 1024 and return the variable.
public static int one() {
}
Write out a series of Java variables.
a variable named foot equal to 12 a variable named fingers equal to 4 a variable named thumb equal to 1 a variable name isPlayerAlive equal to true a variable named speed equal to 55.0 a variable named playerOne equal to a string "Joe" a variable named PI equal to 3.14159 a variable named hand equal to 2 times fingers plus 2 times thumbs
An array is a list of some kind of data. Here is an example of an array of fruits.
String[] fruits = {"Apple", "Orange", "Plum"};
System.out.println( fruits[0] ); // Apple
System.out.println( fruits[1] ); // Orange
System.out.println( fruits[2] ); // PlumNotice how the phrase fruits[0] prints out just Apple from the array. The list of fruits is ordered, and indexed from 0 to 2. We can also find out the length of the array by asking for its length property.
System.out.println( fruits.length ); // 3Now, use a for loop to print out each item of our fruits array.
First, need to build an empty function.
public static void printFruits() {
String[] fruits = {"Apple", "Orange", "Plum"};
}Second, we need to put in a loop that step through each string in the array.
public static void printFruits() {
String[] fruits = {"Apple", "Orange", "Plum"};
for (int i = 0; i < fruits.length; i++) {
}
}And finally, we print out each item in the array.
public static void printFruits() {
String[] fruits = {"Apple", "Orange", "Plum"};
for (int i = 0; i < fruits.length; i++) {
System.out.println( fruits[i] );
}
}Before you try to solve this next problem, spend some time reviewing
Complete the function below to swap two elements in an array and return the result. Your function should take three parameters: An array and two integers. The integers are the indexes of the two elements in the array you should swap.
Example 1:
Input: [7,4,9,3,6,2], 4, 2
Output: [7,4,6,3,9,2],
This problem is a little harder. We need to take a string and copy out the first an last letters, and then return them reversed in order and with a space in between them.
Given a string, create a new string made up of its first and last letters, reversed and separated by a space.
Example Given the word 'bat', return 't b'. Given the word 'motor', return 'r m'.
Function Description Complete the function lastLetters in the editor below.
lastLetters has the following parameter(s): string word: a string to process
Returns: string: a string of two space-separated characters
Constraint 2 ≤ length of word ≤ 100
Below, we will be using a list of numbers. Imagine we have an array of numbers like this:
int[] numbers = {3, 8, 5, 7, 9, 2, 13};Calculate the sum of an array of integers.
Example numbers = {3, 8, 5, 7, 9, 2, 13}
The sum is 3 + 8 + 5 + 7 + 9 + 2 + 13 = 47.
Function Description Complete the function arraySum in the editor below.
arraySum has the following parameter(s): int numbers[n]: an array of integers Returns int: integer sum of the numbers array
This problem expects us to make a function that returns the sum of all the numbers in the array.
How are we going to do this?
Well, the first thing to solve this problem is to setup the function we will be writing.
public static int arraySum(int[] aList) {
}Okay, now we know we have to figure out the result of all the addition. Let’s set up a variable called runningSum and set it to zero, and at the end of the function, return it as the result of the function. This will not give a working program yet, but it is meant to show how you can step through writing the function bit by bit.
public static int arraySum(int[] aList) {
int runningSum = 0;
return runningSum;
}This step by step fashion is very useful to get some of the fundamental pieces of the solution out of the way.
Now, we need to figure out how to step through the array and adding each number we find in there to the runningSum variable. To do that we are going use a loop. This for loop takes the aList parameter and for each item in aList, assigns it to n. We then add n to our runningSum. At the end of the loop, we return the value of runningSum.
public static int arraySum(int[] aList) {
int runningSum = 0;
for (int n : aList) {
runningSum = runningSum + n;
}
return runningSum;
}And when we test it, it should give us the right answer. (Use the REPL to see)
public static int arraySum(int[] aList) {
int runningSum = 0;
for (int n : aList) {
runningSum = runningSum + n;
}
return runningSum;
}
int[] numbers = {3, 8, 5, 7, 9, 2, 13};
System.out.println( arraySum(numbers) ); // answer should be 47.Now, you change the loop used to the for loop with an index, and test it out.
public static int arraySum(int[] aList) {
int runningSum = 0;
// use a loop which has an index *i* and uses *aList[i]* to get each number.
return runningSum;
}
int[] numbers = {3, 8, 5, 7, 9, 2, 13};
System.out.println( arraySum(numbers) ); // answer should be 47.Practice reading user input with Scanner. Try these exercises:
Write a program that asks the user for their name and age, then prints a personalized message.
Scanner scanner = new Scanner(System.in); // Your code here scanner.close();
Create a simple calculator that asks for two numbers and an operation (+, -, *, /) and prints the result.
Use StringBuilder to efficiently build strings:
Create a function that takes an array of words and combines them into a single sentence with spaces between words.
public static String combineWords(String[] words) {
// Use StringBuilder here
}
Create overloaded methods for calculating area:
// Area of a square
public static double area(double side) {
return 0; // Fill this in
}
// Area of a rectangle
public static double area(double length, double width) {
return 0; // Fill this in
}
// Area of a triangle
public static double area(double base, double height, String shape) {
// Only calculate if shape equals "triangle"
return 0; // Fill this in
}
