In: Computer Science
Java Programming
Compound Logic and nested if statement
For a student to be accepted in XYZ College, the student must meet the following requirements:
XYZ college asked you to write a program to implement the above requirements.
Submit:
Using compound logic :
//import package
import java.util.*;
//Java class
public class CompoundLogic {
// main() method
public static void main(String[] args) {
// Object of Scanner class
Scanner sc = new
Scanner(System.in);
// asking user GPA
System.out.print("Enter GPA :
");
// reading GPA
double GPA = sc.nextDouble();
// asking user Family income
System.out.print("Enter Family
income : ");
// reading Family income
double familyIncome =
sc.nextDouble();
sc.nextLine();
// asking user resident
System.out.print("Enter resident :
");
// reading resident
String resident =
sc.nextLine();
//This line is using ternary
operator in java
System.out.println(GPA>=3.75
&& familyIncome>60000 &&
resident.toUpperCase().contentEquals("NEW JERSEY")?"Student
accepted to XYZ college":"Student not accepted to XYZ
college");
}
}
=====================================
Output :
Screen when meets the requirements :
Screen when requirements does not meet :
**********************************************************
using nested if statement :
//import package
import java.util.*;
//Java class
public class nestedifstatement {
// main() method
public static void main(String[] args) {
// Object of Scanner class
Scanner sc = new
Scanner(System.in);
// asking user GPA
System.out.print("Enter GPA :
");
// reading GPA
double GPA = sc.nextDouble();
// asking user Family income
System.out.print("Enter Family
income : ");
// reading Family income
double familyIncome =
sc.nextDouble();
sc.nextLine();
// asking user resident
System.out.print("Enter resident :
");
// reading resident
String resident =
sc.nextLine();
// using nested of checking
if (GPA >= 3.75) {
// when GPS is
greater then 3.75
// checking
family income
if (familyIncome
> 60000) {
// when family income greater than 60000
then
// checking resident
if (resident.toUpperCase().contentEquals("NEW
JERSEY")) {
// when resident is of New
Jersey then
System.out.println("Student
accepted to XYZ college");
}
}
}
}
}
===========================================
Output :