0% found this document useful (0 votes)
2 views

Practice Ques

The document contains multiple Java programming exercises, including classes for managing student information, college entities, handling exceptions, and inventory management. It demonstrates concepts such as inheritance, abstraction, and exception handling through various examples like a student class, beverage classes, and a stock inventory system. Each exercise is structured with class definitions, constructors, and methods to showcase specific functionalities.

Uploaded by

ishikajanit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practice Ques

The document contains multiple Java programming exercises, including classes for managing student information, college entities, handling exceptions, and inventory management. It demonstrates concepts such as inheritance, abstraction, and exception handling through various examples like a student class, beverage classes, and a stock inventory system. Each exercise is structured with class definitions, constructors, and methods to showcase specific functionalities.

Uploaded by

ishikajanit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: ISHIKA

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 2) college different entries


public class College {
public static void main(String[] args) {
Enquiry e=new
Enquiry("78728686","Shraddha","[email protected]",9897,"b.tech","cs");
e.Info();
Roll r=new
Roll("90","Shraddha","[email protected]",98974,"b.tech","cs");
r.Info();
Alumni a=new
Alumni("google","aws","90","Shraddha","[email protected]",9897,"b.tech","cs
");
a.Info();
}
}
abstract class CollegeEntity{
String name;
String email;
double mobile;
String course;
String branch;
public CollegeEntity(String name,String email,double mobile,String
course,String branch ){
this.name=name;
this.email=email;
this.mobile=mobile;
this.course=course;
this.branch=branch;
}
abstract void Info();
}
class Enquiry extends CollegeEntity{
String regno;
public Enquiry(String regno, String name,String email,double
mobile,String course,String branch){
super(name, email, mobile, course, branch);
this.regno=regno;
}
void Info(){
System.out.println("regno: "+regno);
System.out.println("name: "+name);
System.out.println("email:"+email);
System.out.println("mobile: "+mobile);
System.out.println("course: "+course);
System.out.println("branch: "+branch);
}
}
class Roll extends CollegeEntity{
String rollno;
public Roll(String rollno, String name,String email,double
mobile,String course,String branch){
super(name, email, mobile, course, branch);
this.rollno=rollno;
}
void Info(){
System.out.println("regno: "+rollno);
System.out.println("name: "+name);
System.out.println("email:"+email);
System.out.println("mobile: "+mobile);
System.out.println("course: "+course);
System.out.println("branch: "+branch);
}
}
class Alumni extends CollegeEntity{
String company;
String dept;
public Alumni(String company,String dept,String rollno, String
name,String email,double mobile,String course,String branch){
super(name, email, mobile, course, branch);
this.company=company;
this.dept=dept;
}
void Info(){
System.out.println("regno: "+company);
System.out.println("dept: "+dept);
System.out.println("name: "+name);
System.out.println("email:"+email);
System.out.println("mobile: "+mobile);
System.out.println("course: "+course);
System.out.println("branch: "+branch);
}
}

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

Ques: 8)btech student


// public class Demo {

// }
// Base class: Student
class Student {
String name;
int rollno;
String address;

// Constructor for Student class


public Student(String name, int rollno, String address) {
this.name = name;
this.rollno = rollno;
this.address = address;
}
}

// Derived class: Btech_Student


class Btech_Student extends Student {
String department;
int semester;

// Constructor for Btech_Student class


public Btech_Student(String name, int rollno, String address,
String department, int semester) {
// Call the constructor of the base class
super(name, rollno, address);
this.department = department;
this.semester = semester;
}

// Method to display details of Btech_Student


public void display() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollno);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
System.out.println("Semester: " + semester);
}
}

// Demo class to demonstrate the functionality


public class Demo {
public static void main(String[] args) {
// Create an instance of Btech_Student
Btech_Student btechStudent = new Btech_Student("John Doe", 101,
"123 Main St", "Computer Science", 4);

// Display the details of the Btech_Student


btechStudent.display();
}
}

Ques: 9) beverages

public class CafeDemo {


public static void main(String[] args) {
// Create instances of each beverage type
Beverage coffee = new Coffee("Hot", "Cup", 3.50, "Espresso");
Beverage tea = new Tea("Hot", "Mug", 2.50, "Green");
Beverage smoothie = new Smoothie("Cold", "Glass", 4.00,
"Strawberry");

// Display details of each beverage


coffee.displayDetails();
System.out.println();
tea.displayDetails();
System.out.println();
smoothie.displayDetails();
}
}
// Abstract class: Beverage
abstract class Beverage {
String temperature; // temperature of the beverage
String serve; // serving style of the beverage
double price; // price of the beverage

// Constructor for Beverage class


public Beverage(String temperature, String serve, double price) {
this.temperature = temperature;
this.serve = serve;
this.price = price;
}
// Abstract methods to be implemented by subclasses
abstract void displayDetails();
}
// Subclass: Coffee
class Coffee extends Beverage {
String type; // type of coffee (e.g., Espresso, Latte, etc.)

// Constructor for Coffee class


public Coffee(String temperature, String serve, double price,
String type) {
super(temperature, serve, price);
this.type = type;
}

@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.)

// Constructor for Tea class


public Tea(String temperature, String serve, double price, String
flavor) {
super(temperature, serve, price);
this.flavor = flavor;
}

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

Ques: 10) duplicate


import java.util.*;
class DuplicateNumException extends Exception{
public DuplicateNumException(String message){
super(message);
}
}
public class Duplicate {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Set<Integer>set=new HashSet<>();

System.out.println("enter a list of no. type(end) to finish");


while(true){
String input=sc.nextLine();
if(input.equalsIgnoreCase("end")){
break;
}
try{
int num=Integer.parseInt(input);
if(!set.add(num)){
throw new DuplicateNumException("Duplicate not
found "+num);
}
}
catch(NumberFormatException e){
System.out.println("invalid input . please enter
integer");
}
catch(DuplicateNumException e){
System.out.println(e.getMessage());
break;
}
}
System.out.println("input complete");
}

Ques 11) stocks


public class InventoryDemo {
public static void main(String[] args) {
Item i1=new Item("Laptop",90.87,90);
Item i2=new Item("phone",96.78,98);
//display initial
i1.displayItems();
i2.displayItems();
System.out.println("total :"+Item.getItemCount());
// Add stock to item1
System.out.println("======// Add stock to item1");
i1.displayItems();
System.out.println("total items: "+Item.getItemCount());
// Update stock for item2
i2.updateStock(90);
System.out.println("after updating stock");
i2.displayItems();
System.out.println("total :"+Item.itemCount);
}
}
class Item{
String itemname;
double price;
int quantityInStock;
public static int itemCount=0;
Item(String itemname,double price, int quantityInStock){
this.itemname=itemname;
this.price=price;
this.quantityInStock=quantityInStock;
itemCount+=quantityInStock;
}
// Method to add new stock to the inventory
public void addStock(int quantity){
this.quantityInStock+=quantity;
itemCount+=quantity;
}

// Method to update stock quantities


public void updateStock(int quantity){
itemCount-=this.quantityInStock;
this.quantityInStock=quantity;
itemCount+=quantity;
}

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

You might also like