TYBSC COMPUTER SCIENCE java practical slips solution

PUNE UNIVERSITY TYBSC COMPUTER SCIENCE java practical slips

0 522

 

1) Create a class Sphere, to calculate the volume and surface area of sphere.

(Hint : Surface area=4*3.14(r*r), Volume=(4/3)3.14(r*r*r))

class surfaceareaandvolume {
public static void main(String[] args)
{
double r = 5.0, surfacearea = 0.0, volume = 0.0;
surfacearea = 4 * 3.14 * (r * r);
volume = ((double)4 / 3) * 3.14 * (r * r * r);

System.out.println(“surfacearea of sphere =”+ surfacearea);

System.out.println(“volume of sphere =” + volume);
}
}

2.Write a program to accept ‘n’ name of cities from the user and sort them in ascending order.

class sortasc{
public static void main(String[] args) {

int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;

//Displaying elements of original array
System.out.println(“Elements of original array: “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}

//Sort the array in ascending order
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting
System.out.println(“Elements of array sorted in ascending order: “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}
}
}

 

3.Create a class Student (rollno, name ,class, per), to read student information from the console and display them (Using Buffered Reader class).

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Student {
// Attributes of the Student class
private String rollNo;
private String name;
private String studentClass;
private double percentage;

// Constructor
public Student(String rollNo, String name, String studentClass, double percentage) {
this.rollNo = rollNo;
this.name = name;
this.studentClass = studentClass;
this.percentage = percentage;
}

// Method to display student information
public void displayInfo() {
System.out.println(“Roll Number: ” + rollNo);
System.out.println(“Name: ” + name);
System.out.println(“Class: ” + studentClass);
System.out.println(“Percentage: ” + percentage);
}

// Main method to read input and create a Student object
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
// Read student information from console
System.out.print(“Enter Roll Number: “);
String rollNo = reader.readLine();

System.out.print(“Enter Name: “);
String name = reader.readLine();

System.out.print(“Enter Class: “);
String studentClass = reader.readLine();

System.out.print(“Enter Percentage: “);
double percentage = Double.parseDouble(reader.readLine());

// Create a Student object
Student student = new Student(rollNo, name, studentClass, percentage);

// Display student information
student.displayInfo();
} catch (IOException e) {
System.out.println(“An error occurred while reading input.”);
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println(“Invalid input for percentage.”);
e.printStackTrace();
}
}
}

 

 

 

 

4.Write a program for multilevel inheritance such that Country is inherited from Continent. State is inherited from Country. Display the place, State, Country and Continent.

 

class Continent {
protected String continentName;

public Continent(String continentName) {
this.continentName = continentName;
}

public String getContinentName() {
return continentName;
}
}

// Derived class Country inherits from Continent
class Country extends Continent {
protected String countryName;

public Country(String continentName, String countryName) {
super(continentName);
this.countryName = countryName;
}

public String getCountryName() {
return countryName;
}
}

// Derived class State inherits from Country
class State extends Country {
protected String stateName;

public State(String continentName, String countryName, String stateName) {
super(continentName, countryName);
this.stateName = stateName;
}

public String getStateName() {
return stateName;
}
}

// Main class
public class PlaceHierarchy {
public static void main(String[] args) {
// Create an instance of State
State myState = new State(“North America”, “United States”, “California”);

// Display place, state, country, and continent
System.out.println(“Place: ” + myState.getStateName());
System.out.println(“State: ” + myState.getStateName());
System.out.println(“Country: ” + myState.getCountryName());
System.out.println(“Continent: ” + myState.getContinentName());
}
}

 

 

 

5.Define an abstract class Staff with protected members id and name. Define a parameterized constructor. Define one subclass Office Staff with member department. Create n objects of Office Staff and display all details.

abstract class Staff {
protected int id;
protected String name;

// Parameterized constructor
public Staff(int id, String name) {
(link unavailable) = id;
this.name = name;
}

// Abstract method to display details
public abstract void displayDetails();
}

// Subclass OfficeStaff
class OfficeStaff extends Staff {
private String department;

// Parameterized constructor
public OfficeStaff(int id, String name, String department) {
super(id, name);
this.department = department;
}

// Override displayDetails method
@Override
public void displayDetails() {
System.out.println(“ID: ” + id);
System.out.println(“Name: ” + name);
System.out.println(“Department: ” + department);
System.out.println();
}
}

public class Main {
public static void main(String[] args) {
// Create OfficeStaff object
OfficeStaff staff = new OfficeStaff(1, “John Doe”, “HR”);

// Display details
staff.displayDetails();
}
}

 

 

 

 

 

 

Leave A Reply

Your email address will not be published.