You are on page 1of 8

SWE-312 Software Construction and Development SSUET/QR/114

LAB # 2

Good Practices of Programming


OBJECTIVE:
Implementing good code practices and code optimization techniques.
Lab Tasks:

1. Create a design for the mark sheet by taking the runtime value of the
student’s name, total marks, and obtained marks and calculating its
percentage, grade, and GPA. Use good practices of programming that we
have studied and ensure that the outcomes should be presented in a proper
Viewable approach.
Source Code:
import java.util.Scanner;

public class StudentMarkSheet {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input student details


System.out.print("Enter student's name: ");
String studentName = scanner.nextLine();

System.out.print("Enter total marks: ");


double totalMarks = scanner.nextDouble();

System.out.print("Enter obtained marks: ");


double obtainedMarks = scanner.nextDouble();

double percentage = (obtainedMarks / totalMarks) * 100;

char grade;

if (percentage >= 90) {


grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else {
grade = 'F';
SWE-312 Software Construction and Development SSUET/QR/114

double gpa = calculateGPA(percentage);

// Display the mark sheet


System.out.println("\n-------- Mark Sheet --------");
System.out.println("Student Name: " + studentName);
System.out.println("Total Marks: " + totalMarks);
System.out.println("Obtained Marks: " + obtainedMarks);
System.out.println("Percentage: " + percentage + "%");
System.out.println("Grade: " + grade);
System.out.println("GPA: " + gpa);
}

public static double calculateGPA(double percentage) {


if (percentage >= 90) {
return 4.0;
} else if (percentage >= 80) {
return 3.0;
} else if (percentage >= 70) {
return 2.0;
} else if (percentage >= 60) {
return 1.0;
} else {
return 0.0;
}
}
}

Output:

2. Create a class Rectangle with attributes length and width, each of which
defaults to 1. Provide methods that calculate the rectangle’s perimeter and
SWE-312 Software Construction and Development SSUET/QR/114

area. It has set and get methods for both length and width. The set methods
should verify that length and width are each floating-point number larger
than 0.0 and less than 20.0. Write a program to test class Rectangle.
Source Code:
public class Rectangle {
private double length;
private double width;

// Constructor with default values


public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}

// Getter for length


public double getLength() {
return length;
}

// Setter for length with validation


public void setLength(double length) {
if (length > 0.0 && length < 20.0) {
this.length = length;
} else {
System.out.println("Invalid length. Length must be a floating-
point number between 0.0 and 20.0.");
}
}

// Getter for width


public double getWidth() {
return width;
}

// Setter for width with validation


public void setWidth(double width) {
if (width > 0.0 && width < 20.0) {
this.width = width;
} else {
System.out.println("Invalid width. Width must be a floating-
point number between 0.0 and 20.0.");
}
}

// Method to calculate the perimeter


public double calculatePerimeter() {
return 2 * (length + width);
}

// Method to calculate the area


public double calculateArea() {
return length * width;
}
SWE-312 Software Construction and Development SSUET/QR/114

public class RectangleTest {


public static void main(String[] args) {
Rectangle myRectangle = new Rectangle();

// Setting valid length and width


myRectangle.setLength(5.5);
myRectangle.setWidth(3.0);

// Displaying rectangle details


System.out.println("Rectangle Details:");
System.out.println("Length: " + myRectangle.getLength());
System.out.println("Width: " + myRectangle.getWidth());
System.out.println("Perimeter: " +
myRectangle.calculatePerimeter());
System.out.println("Area: " + myRectangle.calculateArea());

// Attempt to set invalid length and width


myRectangle.setLength(25.0); // Should display an error message
myRectangle.setWidth(-1.5); // Should display an error message
}
}
Output:

3. Convert the following class diagram into optimized and meaningful java
code.
SWE-312 Software Construction and Development SSUET/QR/114

Source Code:
SWE-312 Software Construction and Development SSUET/QR/114
SWE-312 Software Construction and Development SSUET/QR/114

Output:
SWE-312 Software Construction and Development SSUET/QR/114

You might also like