In: Computer Science
Write this in java please. I use Eclipse
Write the following two functions. main doesnt need to be filled up.
Function 1:
Write a function called howMany that takes two arguments: an array of integers called arr, and an integer called iTarget. The function should return an integer result. Inside the function write a loop to go through the array and count the number of elements in the array that have the same value as iTarget. At the end it should return the count as the function return value.
Function 2:
Write a function called endsIn that takes two arguments: a String and a character. The function should return true if the string ends in that character or else return false. (Only if the ending character of the string is the character passed in as an argument.)
You sell the book “JAVA for Fools”. Write a program that has you enter a years worth of monthly sales (in terms of money). Create an array of string objects initialized to the month strings. The program should use a loop to prompt you by month from this created array of months and storing the input data (sales numbers for each month) in an array of double. Then the program should find the sum of the array contents and report the total sales for the year.
The Kingdom of Mars, where the unit of currency is the Rock, has the following income tax code:
First 3,000 Rocks: 0% tax
Next 5,000 Rocks: 5% tax
Next 10,000 Rocks: 10% tax
Rocks after 18,000: 15% tax
For example, someone earning 30,000 Rocks would owe (3,000 × 0.00 + 5,000 × 0.05 + 10,000 × 0.10 + 12,000 × 0.15, or) 3,050 Rocks. Write a JAVA program that inputs incomes and calculates the tax owed in the Kingdom of Mars in Rocks.
Test1.java
public class Test1 {
public static void main(String[] args) {
// Test samples
System.out.println(howMany(new
int[] {1,2,3,2,3,1,1,1},1));
System.out.println(endsIn("Hello",'o'));
System.out.println(endsIn("Many",'o'));
}
public static int howMany(int arr[],int iTarget)
{
int count = 0;
// Iterate over array
// if value at index i =
iTarget
// Increment count by 1
for(int i =0;i<arr.length;i++)
{
if(arr[i]==iTarget) {
count+=1;
}
}
// Return count
return count;
}
public static boolean endsIn(String str,char ch)
{
// If char at last index = ch
// return true
// else false
if(str.charAt(str.length()-1)==ch)
{
return
true;
}else {
return
false;
}
}
}
OUTPUT
YearSale.java
import java.util.Scanner;
public class YearSale {
public static void main(String[] args) {
// Scanner class object to take
user input
Scanner scan = new
Scanner(System.in);
// Create string array of
months
String[] months = new String[]
{"January","February","March","April","May","June",
"July","August","September","October","November","December"};
// Create double array for
sales
double sales[] = new
double[12];
// Iterate loop for all month
// take use input
for(int i =
0;i<sales.length;i++) {
System.out.print(months[i]+" sale: ");
sales[i] =
Double.parseDouble(scan.nextLine());
}
// Iterate loop over sales
array
// Add sales to totalSales
double totalSales = 0;
for(int i =
0;i<sales.length;i++) {
totalSales+=sales[i];
}
// Print totalSales
System.out.println("Total sales for
the year is $"+totalSales);
}
}
OUTPUT
Mars.java
import java.util.Scanner;
public class Mars {
public static void main(String[] args) {
// Scanner class object to take
user input
Scanner scan = new
Scanner(System.in);
// Ask user to enter
income
System.out.print("Enter income:
");
double income =
Double.parseDouble(scan.nextLine());
// Assign income to temp
variable
double temp = income - 3000;
double tax = 0;
// If temp is between
0-5000
// apply tax 0.05 on temp
amount
if (temp > 0 && temp
<= 5000) {
tax += temp *
0.05;
}
// If between 5000-10000
// apply tax 0.05 on first
5000
// then subtract 5000 from
temp
// apply tax 0.10 on remaining temp
amount
if (temp > 5000 && temp
<= 18000) {
tax += 5000 *
0.05;
temp = temp -
5000;
tax += temp *
0.10;
}
if (temp > 18000) {
tax += 5000 *
0.05;
temp = temp -
5000;
tax += 10000 *
0.10;
temp = temp -
10000;
tax += temp *
0.15;
}
System.out.println(tax + " rocks
owed");
}
}
OUTPUT