Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Lab :2
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("Afterdiscount:"+discount_amount+"
$("+discount_rate*100+"%)");
}
}
Exercise 1: Write a Java program by using two for loops
to produce the output shown below:
*******
******
*****
****
***
**
*
Solution:
public class JavaExercises
{
public static void main(String[] args)
int i,j;
for(i=0;i<=6;i++){
for(j=1;j<=7-i;j++) System.out.print("*");
System.out.println("");
}
}
Reading single character:
System.in.read() method is used to read a single
character from user’s input value. It will return
numerical value of the input character. Therefore, you
need to convert the value to the character using (char)
conversion statement otherwise you may not
understand what the user input.
Example:
public class Getcha
{
public static void main(String[] args) throws Exception
{
char c;
System.out.print("Enter a character:");
c = (char)System.in.read();
System.out.println("You entered " + c);
}
}
In the sample above, exception must be thrown to pass
the errors to the operating system to handle them. If
exception is not thrown, your program will not be
compiled.
Scanner class:
Scanner is a popular class used to get input from
keyboard. This class has different methods to read
different types of data. Below are the frequently used
methods of the Scanner class.
-next() reads a string value. If the string value contains
white spaces, only the first part of the string is read.
-nextLine() reads a string value.
-nextInt() reads an integer number.
-nextFloat() reads a float number.
-nextDouble() reads a double number.
-nextBool() reads a boolean value.
-nextByte() reads a byte number.
Example:
import java.util.Scanner;
public class ScannerInput {
public static void main(String[] args) {
int qty;
float price;
float amount;
Scanner sc=new Scanner(System.in);
System.out.println("Enter quantity:");
qty=sc.nextInt();
System.out.println("Enter unit price:");
price=sc.nextFloat();
amount=qty*price;
System.out.println("The total sale is "+amount);
sc.close();
}
}