Java File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Lab Record

for
Java Programming and Introduction
of python Lab (BCE-C657)

2022 - 2023
By:
Your Name Enrollment Number Branch
Submitted
to
Mr. Namit Khanduja,
Assistant Professor, Department of Computer Science & Engineering,
Faculty of Engineering & Technology,
Gurukul Kangri Deemed to be University, Haridwar.
Declaration

This is to certify that the programs done in this manual has been developed by me and I have
not copied this file from anywhere.

Signature of Student

Certificate

This is to certify that Mr. YourName of B.Tech VI semester has satisfactorily


completed his experiments for Java programming and Introduction of
python lab in requirement for partial fulfillment of bachelor degree in
Electronics & Communication Engineering prescribed by Faculty of
Engineering & Technology, Gurukula Kangri Deemed to be University,
Haridwar during the year 2022 -2023.

Signature Signature

Internal Examiner External Examiner


Index
S.No Program / Problem Name

1. CEL TO FARENHEIT
2. FARENHEIT TO CEL

3. NESTED CLASSES

4. STACK CLASS WITH EXCPETION

5. STRING SLICING

6. LIST COMPREHENSION

7. DICTIONARY COMPREHENSION

8. BMI CALCULATION

9. DAWOOD ABRAHIM

10. PACKAGE DELIVARY

List of Programs

1) Write a program to convert a given Celsius temperature to Fahrenheit.


2) Write a program to convert a given Fahrenheit temperature to Celsius.
3) Write four programs explaining inner and nested class of each type.
4) Write a program to imitate the Stack Data structure, it should be a menu driven program to
insert, remove, peek and display the contents of the Stack. Name the class as Stack, have proper
constructors to initialize its data members, the user should be prompted to input the size of
stack at the startup of the program. The program should have the functionality to check the
overflow and underflow exceptions and to display appropriate messages once the exceptions
occur. Also demonstrate the program using a Driver code.
5) Write a program to demonstrate string slicing in python for negative and positive slicing.
6) Write a program in python to demonstrate different ways to create List and List comprehension.
7) Write a program in python to demonstrate different ways to create Dictionary and Dictionary
comprehension.
8) Create a BMI calculation application that reads the user's weight in pounds or kg and height in
inches or meter. Calculate and display the user's BMI . the should also display the catagory like :
1> underweight if the BMI is less than 18.5
2> normal if the BMI is in between 18.5 to 24.9 3>
overweight if the BMI is greater than 30
formula use for the BMI calculation is : : BMI= (weight(pounds)*703)/height(inches) and
BMI=(weight(kg))/height(m)
9) The famous gangster Dawood Abrahim is moving to Islamabad. He has a very big family there,
all of them living on khan Avenue. Since he will visit all his relatives very often, he wants to find a
house close to them. Indeed, Dawood wants to minimize the total distance to all of his relatives
and has blackmailed you to write a program that solves his problem.
Input
The input consists of several test cases. The first line contains the number of test cases. For each
test case you will be given the integer number of relatives r (0 < r < 500) and the street numbers
(also integers) s1, s2, . . . , si, . . . , sr where they live (0 < si < 30, 000). Note that several relatives
might live at the same street number.
Output
For each test case, your program must write the minimal sum of distances from the optimal
Dawood’s house to each one of his relatives. The distance between two street numbers si and sj
is dij = |si − sj |.
Sample Input 2
224
3246
Sample Output
2
4
10) Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of different shipping
options, each with specific costs associated. Create an inheritance hierarchy to represent various
types of packages. Use class Package as the base class of the hierarchy, then include classes
TwoDayPackage and OvernightPackage that derive from Package. Base class Package should
include data members representing the name, address, city, state and ZIP code for both the
sender and the recipient of the package, in addition to data members that store the weight (in
ounces) and cost per ounce to ship the package. Package’s constructor should initialize these
data members. Ensure that the weight and cost per ounce contain positive values. Package
should provide a public member function calculateCost that returns a double indicating the cost
associated with shipping the package. Package’s calculateCost function should determine the
cost by multiplying the weight by the cost per ounce. Derived class TwoDayPackage should
inherit the functionality of base class Package, but also include a data member that represents a
flat fee that the shipping company charges for two-day-delivery service. TwoDayPackage’s
constructor should receive a value to initialize this data member. TwoDayPackage should
redefine member function calculateCost so that it computes the shipping cost by adding the flat
fee to the weight-based cost calculated by base class Package’s calculateCost function. Class
OvernightPackage should inherit directly from class Package and contain an additional data
member representing an additional fee per ounce charged for overnightdelivery service.
OvernightPackage should redefine member function calculateCost so that it adds the additional
fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test
program that creates objects of each type of Package and tests member function calculateCost.
Program No 1

Problem Statement:
Write a program to convert a given Celsius temperature to Fahrenheit.
Code:
import java.util.Scanner;

public class CelsiusToFahrenheit {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter temperature in Celsius: ");

double celsius = input.nextDouble();

double fahrenheit = (celsius * 9/5) + 32;

System.out.println(celsius + " degrees Celsius is equal to " + fahrenheit + " degrees


Fahrenheit.");

}
Input:

Enter temperature in Celsius: 25

Output:

25.0 degrees Celsius is equal to 77.0 degrees Fahrenheit.


Program No 2

Problem Statement:
Write a program to convert a given Fahrenheit temperature to Celsius.

Code:
import java.util.Scanner;

public class FahrenheitToCelsius {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter temperature in Fahrenheit: ");

double fahrenheit = scanner.nextDouble();

double celsius = (fahrenheit - 32) * 5 / 9;

System.out.printf("%.2f degrees Fahrenheit is equal to %.2f degrees Celsius", fahrenheit,


celsius);

}
Input:

Enter temperature in Celsius: 68

Output:

68.0 degrees Celsius is equal to 20.0 degrees Fahrenheit.


Program No 3

Problem Statement:

You might also like