﻿ Java exercises and solutions: compound operators

# Java exercises and solutions: compound operators

## Java compound operators

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);

}

}

### Comments santoshisnt it 72???2017-07-26 esirbotaCreate a class for students. The class must contain each of the student's name(String), lD(int) and status(int). The status indicates the student's class standing: 1 for freshman, 2 for sophomore, 3 for junior, and 4 for senior. 2017-07-23 Andrei72 it is2017-05-24 SKRajesh, how is it 35? Shouldn't it be 72?2016-11-24 RAJESHIn the second example, the sample output is calculated wrongly. The final interest earned value should be 35 not 71.2016-08-07 Mehdiyou are right Jack the Exercise two is incorrect, he rushed the exercise this might be a solution: import java.util.*; public class exercise8 { public static void main(String[] args) { Calculateinterest(); } static void Calculateinterest(){ 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 the yearly interest rate: "); rate=sc.nextFloat(); interest_earned = (amount_dep * (rate/100))*(amount_dep/100); //total amount with interests System.out.print("Enter income tax rate"); tax=sc.nextFloat(); tax_amount=(interest_earned*(tax/100))*(interest_earned/100); interest_earned-=tax_amount; // the final interest earned System.out.println("The interest earned in the year: "+interest_earned); } } 2016-07-14 JackThere's a mistake in the exercise 2.. When the tax amount is deducted from the interest gained in the final result the deduction was done using the user given "tax" input instead of using its percentage from the deposit..So the result is not accurate as shown in the example.. 2016-01-14

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.