Java exercises and solutions: if else and logical operators


if else and logical operators

Exercise 1: Write a Java program that determines a student’s grade.

The program will read three types of scores(quiz, mid-term, and final scores) and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F

See the example output below:

Quiz score: 80

Mid-term score: 68

Final score: 90

Your grade is B.

Solution:


import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
showGrade();
}

static void showGrade(){

float quiz_score, mid_score,final_score,avg;
Scanner sc=new Scanner(System.in);
System.out.print("Quiz score:");
quiz_score=sc.nextFloat();
System.out.print("Mid-term score:");
mid_score=sc.nextFloat();
System.out.print("Final score:");
final_score=sc.nextFloat();
avg=(quiz_score+mid_score+final_score)/3;

if(avg>=90) System.out.println("Your grade A.");
else if((avg>=70) && (avg<90)) System.out.println("Your grade B.");
else if((avg>=50) && (avg<70)) System.out.println("Your grade C.");
else if(avg<50) System.out.println("Your grade F.");
else System.out.println("Invalid");


 }

}

Exercise 2: Write a Java program to calculate the revenue from a sale based on the unit price and quantity of a product input by the user.

The discount rate is 10% for the quantity purchased between 100 and 120 units, and 15% for the quantity purchased greater than 120 units. If the quantity purchased is less than 100 units, the discount rate is 0%. See the example output as shown below:

Enter unit price: 25

Enter quantity: 110

The revenue from sale: 2475.0$

After discount: 275.0$(10.0%)

Solution:


import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
calculateSale();
}

static void calculateSale(){

float unitprice=0f;
int quantity=0;
float revenue=0f;
float discount_rate=0f, discount_amount=0f;

Scanner sc=new Scanner(System.in);
System.out.print("Enter unit price:");
unitprice=sc.nextFloat();
System.out.print("Enter quantity:");
quantity=sc.nextInt();

if(quantity<100)
revenue=unitprice*quantity;
else if(quantity>=100 && quantity<=120)
{
discount_rate=(float)10/100;
revenue=unitprice*quantity;
discount_amount=revenue*discount_rate;
revenue-=discount_amount;
}

else if(quantity>120)
{
discount_rate=(float)15/100;
revenue=unitprice*quantity;
discount_amount=revenue*discount_rate;
revenue-=discount_amount;
}

System.out.println("The revenue from sale:"+revenue+"$");
System.out.println("After discount:"+discount_amount+"$("+discount_rate*100+"%)");




 }

}





Comments

William  comment

 William

Can someone help me out with this programme........
/*Write a java program that inputs the salary below

PAYE AMOUNT

>10,000000
(25000+30%*(SALARY-410,000+10%(SALARY-10,000000)

>410,000
(25000+30%*(SALARY-410,000)

>335,000
(10,000+20%*(SALARY-335000)

>235000
(10%*(SALARY-235000)

<235000
0
*/

import java.util.Scanner;

public class PAYE
{

public static void main(String Args[]){

computePAYE();

}

public static void computePAYE(){

double SALARY=0;
double paye=0;

Scanner salary=new Scanner(System.in);

System.out.println("How much salary do you earn?!");
SALARY = salary.nextInt();


if(SALARY > 10000000){
paye=((25000+((30/100)*((SALARY-410000))+((10/100)*(SALARY-10000000)))));
System.out.println("\nPAYE will be "+paye);
}

else if(SALARY > 410000){
paye=(25000+(30/100*(SALARY-410000)));
System.out.println("\nPAYE will be "+paye);
}

else if(SALARY > 335000){
paye=(10000+(20/100*(SALARY-335000)));
System.out.println("\nPAYE will be "+paye);
}

else if(SALARY > 235000){
paye=((10/100)*(SALARY-235000));
System.out.println("\nPAYE will be "+paye);
}

else{
paye=0;
System.out.println("\nPAYE will be "+paye);
}
}
}


2018-03-18
tayo comment

 tayo

Can some one help with these programe please below:

write a program that reads number of gallons of gas in the tank of a car, the number of miles per gallon, and the price of gas per gallon. the program then compute and prints:
1 - How far (in miles) the car can travel with the gas
2 - The cost of travelling 250 miles with the car


2017-07-11
tayo comment

 tayo

Can some one help with these programe please below:

write a program that reads number of gallons of gas in the tank of a car, the number of miles per gallon, and the price of gas per gallon. the program then compute and prints:
1 - How far (in miles) the car can travel with the gas
2 - The cost of travelling 250 miles with the car


2017-07-11
tayo comment

 tayo

a bus company organizes a tour. each participant would pay $2500. Given the number of participants, write a program that calculate and prints the revenue the company will generate for the tour.

Can some one help with these programe please


2017-07-11
tayo comment

 tayo

a bus company organizes a tour. each participant would pay $2500. Given the number of participants, write a program that calculate and prints the revenue the company will generate for the tour.

Can some one help with these programe please


2017-07-11
tayo comment

 tayo

a bus company organizes a tour. each participant would pay $2500. Given the number of participants, write a program that caluculate and prints the revenue the company will generate for the tour.


2017-07-11
tayo comment

 tayo

a bus company organizes a tour. each participant would pay $2500. Given the number of participants, write a program that caluculate and prints the revenue the company will generate for the tour.


2017-07-11
kang comment

 kang

import java.util.Scanner;

public class UnitDiscount
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter unit price ");
double uprice = in.nextDouble();
System.out.println("Enter the no of units ");
int nounit = in.nextInt();

if(nounit>100&&nounit<120)
uprice = uprice - uprice*0.1;
if(nounit>120)
uprice = uprice - uprice*0.15;
double totalprice= uprice*nounit;
System.out.println("the total unit price: " +totalprice);
}
}


2017-02-13
satya comment

 satya

package exercise2;

import java.util.Scanner;

public class Exercise2 {

public static void main(String[] args) {
calculateRevenue();
}
public static void calculateRevenue(){
int unitPrice,quantity;
double discountRate;
double totalRevenue;
Scanner sc=new Scanner(System.in);
System.out.println("enter unit price");
unitPrice=sc.nextInt();
Scanner sc1=new Scanner(System.in);
System.out.println("enter quantity");
quantity=sc1.nextInt();
if(quantity>=100 && quantity<=120){
discountRate=(quantity*unitPrice)*0.10;
totalRevenue=(quantity*unitPrice);
System.out.println("total revenue is "+totalRevenue);
System.out.println("total with a 10% discount " +discountRate);
}
else if(quantity>120){
discountRate=(quantity*unitPrice)*0.15;
totalRevenue=(quantity*unitPrice);
System.out.println("total revenue is "+totalRevenue);
System.out.println("total with a 15% discount " +discountRate);
}
else if(quantity<100){
discountRate=(quantity*unitPrice);
totalRevenue=(quantity*unitPrice);
System.out.println("total revenue is "+totalRevenue);
System.out.println("total with no discount " +discountRate);
}

}
}


2016-12-20
Dev  comment

 Dev

What would the code be in C++ ?


2016-02-25
Fransake comment

 Fransake

import java.util.Scanner;
public class sales {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the price unit :");
double price = in.nextDouble();
System.out.print("Enter the quantity: ");
double quantity = in.nextDouble();
double money=0;
double revenue= 0;
if(quantity >= 100 && quantity <= 120 ){
money = (price*quantity)*(10.0/100);
revenue = (price*quantity)- money;
}else{
if(quantity > 120){
money = (price*quantity)*(15.0/100);
revenue = (price*quantity)- money;
} else{
if(quantity < 100){
money = (price*quantity);
revenue = money;
}
}
}
System.out.println("the discount is " + money+ "$");
System.out.println("the revenue is " + revenue + "$");
in.close();
}

}


2016-02-03
Fransake comment

 Fransake

import java.util.Scanner;
public class sales {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the price unit :");
double price = in.nextDouble();
System.out.print("Enter the quantity: ");
double quantity = in.nextDouble();
double money=0;
double revenue= 0;
if(quantity >= 100 && quantity <= 120 ){
money = (price*quantity)*(10.0/100);
revenue = (price*quantity)- money;
}else{
if(quantity > 120){
money = (price*quantity)*(15.0/100);
revenue = (price*quantity)- money;
} else{
if(quantity < 100){
money = (price*quantity);
revenue = money;
}
}
}
System.out.println("the discount is " + money+ "$");
System.out.println("the revenue is " + revenue + "$");
in.close();
}

}


2016-02-03
elmagek comment

 elmagek

A7la mesa 3la ergala


2016-01-01
nouh comment

 nouh

package sales;
import java.util.Scanner;


public class Sales {


public static void main(String[] args) {
sales();
}
public static void sales(){
Scanner p=new Scanner(System.in);
System.out.println("enter the price of quantity");
float cach=p.nextFloat();
System.out.println("enter thenumber of quantity");
Float num=p.nextFloat();

if ((cach>100)&&(cach<120)){
float money=cach*10/100f;
System.out.println("the revenu befor discound = "+cach*num);
System.out.println("the rat of discount is = "+((cach*num)-(money*num)));
System.out.println("the renenu aftrr discound = "+money*num);

}
else
System.out.println("the price of sale ="+cach*num);

if (cach>120){
float money=cach*15/100f;
System.out.println("the price befor discountis =" +cach*num);
System.out.println("the revrnu after discount is ="+money*num);
}

}




}


2016-01-01
vikas shivawedi comment

 vikas shivawedi



import java.util.Scanner;


public class Cashback {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("amount is");
int a=sc.nextInt();
System.out.println("customer give");
int b=sc.nextInt();
System.out.println("back amount is::"+(b-a));

}
}


2015-07-05
Dalim Ali comment

 Dalim Ali

I think it will helpful more than the above code.....................


import java.util.Scanner;


public class Revenue {


public static void main(String[] args) {
change();

}

private static void change() {
float x,z,y,Due; //x=unit price, z=change
//y=cash
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.print("Total Amount Purchased = ");
x=sc.nextFloat();
System.out.print("Cash Given = ");
y=sc.nextFloat();

if (x > y) {
z = x - y ;
System.out.println("Your have to give more = " + z +" tk");

}
// else if (x == y) {
// Due = x - y ;
// System.out.println("Due = "+ Due);
// System.out.println("Thank you! Come again!!!");
//
//}


// if u want ,u can use these lines also
else {
z = y - x ;
System.out.println("Your change is = " +z);
Due = (y - x) - z ;
System.out.println("Due = " + Due);

System.out.println("Thank you! Come again!!!");
}
}


}


2015-05-26
Hernan comment

 Hernan

i'm just new in Java coding.. sorry for my long codes..lol
________________________________

import java.util.Scanner;


public class PaymentChangeExercise {


public static void main(String[] args) {
change();

}

private static void change() {
float x,z,y; //x=unit price, z=change
//y=cash
Scanner sc=new Scanner(System.in);
System.out.print("Total Amount Purchased:");
x=sc.nextFloat();
System.out.print("Cash Given:");
y=sc.nextFloat();

z=-(x-y);

System.out.println("Your change is:" +z);
System.out.println("Thank you! Come again!!!");

}
}


2014-08-05
NURMD comment

 NURMD

NEED TO THE SOLUTION LIKE

TOTAL AMOUNT(PURCHAGE) = 490TK

CASH GIVEN = 500TK
RETURN =10TK

DUE =0TK

NEED TO THE JAVA CODE FOR THIS PROGRAM


2014-06-23



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.