0% found this document useful (0 votes)
5 views6 pages

Java Programming Best Practices Guide

The document outlines best programming practices, emphasizing the use of variables, avoidance of hard coding, proper naming conventions, and indentation. It includes sample Java programs demonstrating these practices, such as displaying results and calculating travel distances. Additionally, it provides a list of practice programming exercises to reinforce these concepts.

Uploaded by

imvenugopal08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Programming Best Practices Guide

The document outlines best programming practices, emphasizing the use of variables, avoidance of hard coding, proper naming conventions, and indentation. It includes sample Java programs demonstrating these practices, such as displaying results and calculating travel distances. Additionally, it provides a list of practice programming exercises to reinforce these concepts.

Uploaded by

imvenugopal08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Best Programming Practice

1.​ All values as variables including Fixed, User Inputs, and Results
2.​ Avoid Hard Coding of variables wherever possible
3.​ Proper naming conventions for all variables
String name = "Eric";
double height = [Link]();
double totalDistance = distanceFromToVia + distanceViaToFinalCity;
4.​ Proper Program Name and Class Name
5.​ Follow proper indentation

1.​ Sample Program 1 - Write a program to display Sam with Roll Number 1, Percent Marks
99.99, and the result ‘P’ indicates Pass(‘P’) or Fail (‘F’).
IMP => Follow Good Programming Practice demonstrated below in all Practice Programs

Java
// Creating Class with name DisplayResult indicating the purpose is to display
// result. Notice the class name is a Noun.
class DisplayResult {
public static void main(String[] args) {

// Create a string variable name and assign value Sam


String name = "Sam";

// Create a int variable rollNumber and assign value 1


int rollNumber = 1;

// Create a double variable percentMarks and assign value 99.99


double percentMarks = 99.99;

// Create a char variable result and assign value 'P' for pass
char result = 'P';

// Display the result


[Link]("Displaying Result:\n" +name+ " with Roll Number " +
rollNumber+ " has Scored " +percentMarks+
"% Marks and Result is " +result);
}
}

1
2.​ Sample Program 2 - Eric Travels from Chennai to Bangalore via Vellore. From Chennai to
Vellore distance is 156.6 km and the time taken is 4 Hours and 4 Mins and from Vellore to
Bangalore is 211.8 km and will take 4 Hours and 25 Mins. Compute the total distance and
total time from Chennai to Bangalore

Java
// Create TravelComputation Class to compute the Distance and Travel Time

class TravelComputation {

public static void main(String[] args) {

// Create a variable name to indicate the person traveling


String name = "Eric";

// Create a variable fromCity, viaCity and toCity to indicate the city


// from city, via city and to city the person is travelling
String fromCity = "Chennai", viaCity = "Velore", toCity = "Bangalore";

// Create a variable distanceFromToVia to indicate the distance


// between the fromCity to viaCity
double distanceFromToVia = 156.6;

// Create a variable timeFromToVia to indicate the time taken to


// travel from fromCity to viaCity in minutes
int timeFromToVia = 4 * 60 + 4;

// Create a variable distanceViaToFinalCity to indicate the distance


// between the viaCity to toCity
double distanceViaToFinalCity = 211.8;

// Create a variable timeViaToFinalCity to indicate the time taken to


// travel from viaCity to toCity in minutes
int timeViaToFinalCity = 4 * 60 + 25;

// Create a variable totalDistance to indicate the total distance


// between the fromCity to toCity
double totalDistance = distanceFromToVia + distanceViaToFinalCity;

// Create a variable totalTime to indicate the total time taken to


// travel from fromCity to toCity in minutes
int totalTime = timeFromToVia + timeViaToFinalCity;

2
// Print the travel details
[Link]("The Total Distance travelled by " + name + " from " +
fromCity + " to " + toCity + " via " + viaCity +
" is " + totalDistance + " km and " +
"the Total Time taken is " + totalTime + " minutes");
}
}

3
Level 1 Practice Programs
1.​ Write a program to find the age of Harry if the birth year is 2000. Assume the Current Year is
2024
I/P => NONE
O/P => Harry's age in 2024 is ___

2.​ Sam’s mark in Maths is 94, Physics is 95 and Chemistry is 96 out of 100. Find the average
percent mark in PCM
I/P => NONE
O/P => Sam’s average mark in PCM is ___
3.​ Create a program to convert the distance of 10.8 kilometers to miles.
Hint: 1 km = 1.6 miles
I/P => NONE
O/P => The distance ___ km in miles is ___
4.​ Create a program to calculate the profit and loss in number and percentage based on the
cost price of INR 129 and the selling price of INR 191.
Hint =>
a.​ Use a single print statement to display multiline text and variables.
b.​ Profit = selling price - cost price
c.​ Profit Percentage = profit / cost price * 100
I/P => NONE
O/P =>
The Cost Price is INR ___ and Selling Price is INR ___
The Profit is INR ___ and the Profit Percentage is ___
5.​ Suppose you have to divide 14 pens among 3 students equally. Write a program to find how
many pens each student will get if the pens must be divided equally. Also, find the remaining
non-distributed pens.
Hint =>
a.​ Use Modulus Operator (%) to find the reminder.
b.​ Use Division Operator to find the Quantity of pens
I/P => NONE
O/P => The Pen Per Student is ___ and the remaining pen not distributed is ___

4
6.​ The University is charging the student a fee of INR 125000 for the course. The University is
willing to offer a discount of 10%. Write a program to find the discounted amount and
discounted price the student will pay for the course.
Hint =>
a.​ Create a variable named fee and assign 125000 to it.
b.​ Create another variable discountPercent and assign 10 to it.
c.​ Compute discount and assign it to the discount variable.
d.​ Compute and print the fee you have to pay by subtracting the discount from the fee.
O/P => The discount amount is INR ___ and final discounted fee is INR ___
7.​ Write a Program to compute the volume of Earth in km^3 and miles^3
Hint => Volume of a Sphere is (4/3) * pi * r^3 and radius of earth is 6378 km
O/P => The volume of earth in cubic kilometers is ____ and cubic miles is ____
8.​ Create a program to convert distance in kilometers to miles.
Hint =>
a.​ Create a variable km and assign type as double as in double km;
b.​ Create Scanner Object to take user input from Standard Input that is the Keyboard as in
Scanner input = new Scanner([Link]);
c.​ Use Scanner Object to take user input for km as in km = [Link]();
d.​ Use 1 mile = 1.6 km formulae to calculate miles and show the output
I/P => km
O/P => The total miles is ___ mile for the given ___ km
9.​ Write a new program similar to the program # 6 but take user input for Student Fee and
University Discount
Hint =>
a.​ Create a variable named fee and take user input for fee.
b.​ Create another variable discountPercent and take user input.
c.​ Compute the discount and assign it to the discount variable.
d.​ Compute and print the fee you have to pay by subtracting the discount from the fee.
I/P => fee, discountPrecent
O/P => The discount amount is INR ___ and final discounted fee is INR ___
10.​Write a program that takes your height in centimeters and converts it into feet and inches
Hint => 1 foot = 12 inches and 1 inch = 2.54 cm
I/P => height
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___

5
11.​Write a program to create a basic calculator that can perform addition, subtraction,
multiplication, and division. The program should ask for two numbers (floating point) and
perform all the operations
Hint =>
a.​ Create a variable number1 and number 2 and take user inputs.
b.​ Perform Arithmetic Operations of addition, subtraction, multiplication and division and
assign the result to a variable and finally print the result
I/P => number1, number2
O/P => The addition, subtraction, multiplication and division value of 2 numbers ___ and ___
is ___, ____, ____, and ___
10.​Write a program that takes the base and height to find area of a triangle in square inches
and square centimeters
Hint => Area of a Triangle is ½ * base * height
I/P => base, height
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___
11.​Write a program to find the side of the square whose parameter you read from user
Hint => Perimeter of Square is 4 times side
I/P => perimeter
O/P => The length of the side is ___ whose perimeter is ____
12.​Write a program the find the distance in yards and miles for the distance provided by user in
feets
Hint => 1 mile = 1760 yards and 1 yard is 3 feet
I/P => distanceInFeet
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___
15.​Write a program to input the unit price of an item and the quantity to be bought. Then,
calculate the total price.
Hint => NA
I/P => unitPrice, quantity
O/P => The total purchase price is INR ___ if the quantity ___ and unit price is INR ___
16.​Create a program to find the maximum number of handshakes among N number of
students.
Hint =>
a.​ Get integer input for numberOfStudents variable.
b.​ Use the combination = (n * (n - 1)) / 2 formula to calculate the maximum number of
possible handshakes.
c.​ Display the number of possible handshakes.

You might also like