Untitled
unknown
plain_text
25 days ago
3.0 kB
4
Indexable
Never
/* Program Name:Subject.java Programmer: IAN V. YBAÑEZ Date Written: September 15, 2024 */ public class Subject { // Variables for subject information private String code; private String desc; private int lec_units; private int lab_units; // Constructor without parameters (default values) public Subject() { this.code = "000"; this.desc = "no desc"; this.lec_units = 0; this.lab_units = 0; } // Constructor with four parameters public Subject(String code, String desc, int lec_units, int lab_units) { this.code = code; this.desc = desc; this.lec_units = lec_units; this.lab_units = lab_units; } // Method to calculate total units (lec_units + lab_units) public int getTotalUnits() { return this.lec_units + this.lab_units; } // Method to display the subject information public void displaySubjectInfo() { System.out.printf("%-10s %-30s %-5d %-5d %-5d\n", code, desc, lec_units, lab_units, getTotalUnits()); } public static void main(String[] args) { // Creating a subject object with default values Subject defaultSubject = new Subject(); System.out.println("Subject Default Values"); System.out.println("---------------------------------------"); System.out.println("Code : " + defaultSubject.code); System.out.println("Description : " + defaultSubject.desc); System.out.println("Lecture units : " + defaultSubject.lec_units); System.out.println("Laboratory units : " + defaultSubject.lab_units); System.out.println("Total units : " + defaultSubject.getTotalUnits()); System.out.println("---------------------------------------"); // Creating subjects with parameterized constructor Subject prog113 = new Subject("Prog113", "Basic Programming", 2, 1); Subject net113 = new Subject("Net113", "Fundamentals of Networking", 2, 1); Subject eng1113 = new Subject("Eng1113", "English 1", 3, 0); Subject hum113 = new Subject("Hum113", "Life and Works of Rizal", 3, 0); Subject pathFit1 = new Subject("PathFit1", "Physical Education 1", 3, 0); // Displaying the subject information in a table format System.out.println("Subject Information"); System.out.println("+-----------+--------------------------------+-----+-----+-----+"); System.out.printf("| %-9s | %-30s | %-3s | %-3s | %-3s |\n", "Code", "Description", "Lec", "Lab", "Total"); System.out.println("+-----------+--------------------------------+-----+-----+-----+"); prog113.displaySubjectInfo(); net113.displaySubjectInfo(); eng1113.displaySubjectInfo(); hum113.displaySubjectInfo(); pathFit1.displaySubjectInfo(); System.out.println("+-----------+--------------------------------+-----+-----+-----+"); } }
Leave a Comment