Monday, January 25, 2016

Java Core Exercise 3: compound operators

Java programming Exercise 1:  Write Java program to allow the user to input two float values and then the program adds the two values t... thumbnail 1 summary
Java programming
Exercise 1: Write Java program to allow the user to input two float values and then the program adds the two values together. The result will be assigned to the first variable.
Enter value a:12.5
The value of a before adding is 12.5.
Enter value b:34.9
The value of a after adding is 47.4.
Solution:

import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
  caculateValues();
}
static void caculateValues(){
  float a;
  float b;
  Scanner sc=new Scanner(System.in);
  System.out.print("Enter a:");
  a=sc.nextFloat();
  System.out.println("The value of a before adding:"+a);
  System.out.print("Enter b:");
  b=sc.nextFloat();
  a+=b;
  System.out.println("The value of a after adding:"+a);
  }
}

Exercise 2: Write Java program to allow the user to input the amount of deposit, yearly interest rate (percentage), and income tax(percentage). Then the program will calculate the amount of interest that the person earns in the year. See the example output below:
The amount of deposit: 1000
Yearly interest rate: 7.5%
Income tax rate: 4%
The amount of interest earned in the year:71.0
Solution:
import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
  caculateInterest();
}
static void caculateInterest(){
  float amount_dep, rate, tax, interest_earned, tax_amount;
  Scanner sc=new Scanner(System.in);
  System.out.print("Enter the amount of deposit:");
  amount_dep=sc.nextFloat();
  System.out.print("Enter yearly interest rate:");
  rate=sc.nextFloat();
  interest_earned=amount_dep*(rate/100); //amount of interest before tax calculation
  System.out.print("Enter income tax rate:");
  tax=sc.nextFloat();
  tax_amount=interest_earned*(tax/100);
  interest_earned-=tax; //the final interest earned
  System.out.println("The interest earned in the year:"+interest_earned);
}
}

No comments

Post a Comment