Practice Ques
Practice Ques
Roll.No: 2200291520090
PRACTICE QUES
Ques 1) Student
import java.util.*
public class Student{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter name:");
String name=sc.nextLine();
System.out.println("enter gender(M),(F),(T)");
char g=sc.next().charAt(0);
System.out.println("enter avg marks:");
double marks=sc.nextDouble();
String fullGender="";
switch(g){
case 'M':
fullGender="MALE";
break;
case 'F':
fullGender="FEMALE";
break;
case 'T':
fullGender="TRANS GENDER";
break;
default:
fullGender="invaid";
break;
}
String grades;
if(marks>80){
grades="A+";
}else if(marks>60){
grades="A";
}else if(marks>50){
grades="B+";
}else{
grades="C";
}
System.out.println("Name: "+name);
System.out.println("avg marks: "+marks);
System.out.println("gender: "+fullGender);
System.out.println("grades: "+grades);
}
}
Ques 3)divide by 0
public class Division {
public static void main(String[] args) {
int a=5;
int b=0;
try{
int res=a/b;
System.out.println("try block");
}
catch(ArithmeticException e){
e.printStackTrace();
System.out.println("catch error");
}
}
}
// }
// Base class: Student
class Student {
String name;
int rollno;
String address;
Ques: 9) beverages
@Override
void displayDetails() {
System.out.println("Beverage: Coffee");
System.out.println("Type: " + type);
System.out.println("Temperature: " + temperature);
System.out.println("Serve: " + serve);
System.out.println("Price: $" + price);
}
}
// Subclass: Tea
class Tea extends Beverage {
String flavor; // flavor of tea (e.g., Green, Black, Herbal,
etc.)
@Override
void displayDetails() {
System.out.println("Beverage: Tea");
System.out.println("Flavor: " + flavor);
System.out.println("Temperature: " + temperature);
System.out.println("Serve: " + serve);
System.out.println("Price: $" + price);
}
}
// Subclass: Smoothie
class Smoothie extends Beverage {
String fruit; // main fruit ingredient of the smoothie
// Constructor for Smoothie class
public Smoothie(String temperature, String serve, double price,
String fruit) {
super(temperature, serve, price);
this.fruit = fruit;
}
@Override
void displayDetails() {
System.out.println("Beverage: Smoothie");
System.out.println("Main Fruit: " + fruit);
System.out.println("Temperature: " + temperature);
System.out.println("Serve: " + serve);
System.out.println("Price: $" + price);
}
}
//getters
public String getItemName(){
return this.itemname;
}
public double getPrice(){
return this.price;
}
public int getQuantityInStock(){
return this.quantityInStock;
}
public static int getItemCount(){
return itemCount;
}
public void displayItems(){
System.out.println("iem name: "+itemname);
System.out.println("price: "+price);
System.out.println("quanstock: "+quantityInStock);
}
}