Question

In: Computer Science

Write this in java please. I use Eclipse Write the following two functions. main doesnt need...

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.

Solutions

Expert Solution

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


Related Solutions

I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
I need this in Java please: Behaviors. In the context of OOP, functions are called methods...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants to demonstrate its political correctness by applying the Supreme Court’s “Separate but equal is inherently unequal” doctrine to gender as well as race. As such, the university has decided that both genders will use the same bathroom facilities. However, in order to preserve some tradition, it decrees that when a woman is in the bathroom, only other women may enter, and when a man...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
Please use python and run in IDLE Question 1 Write functions that do the following: i)...
Please use python and run in IDLE Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii)...
Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT