0% found this document useful (0 votes)
47 views8 pages

Marksheet and Matrix Operations in Java

The document contains a question and solution regarding developing a program to prepare a student mark sheet with inputs read from the keyboard. It then contains a second question and solution regarding developing a program using function overloading to add integer, floating point, and double precision matrices. The third question asks to write the output of a sample code with a static block and main method.

Uploaded by

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

Marksheet and Matrix Operations in Java

The document contains a question and solution regarding developing a program to prepare a student mark sheet with inputs read from the keyboard. It then contains a second question and solution regarding developing a program using function overloading to add integer, floating point, and double precision matrices. The third question asks to write the output of a sample code with a static block and main method.

Uploaded by

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

ASSIGNMENT (SOLUTION)

Ques 1 - Develop a program to prepare the mark sheet of the university


examination with the following items read from the keyboard.
i. Name of student
ii. Roll number
iii. Subject name
iv. Subject code
v. Internal marks
vi. External marks
Design a base class consisting of the data members such as name
of the student, roll number and subject name. The derived class
consists of the data members, viz. subject code, internal marks,
and external marks.
Ans 1 - import [Link];
class Base{
    String name;
    int roll;
    String subject;
}
class Child extends Base{
    String subjectCode;
    int internalMarks;
    int externalMarks;
    void setData(String n, int r, String sub, String subCode, int in
tMarks, int extMarks){
        name = n;
        roll = r;
        subject = sub;
        subjectCode = subCode;
        internalMarks = intMarks;
        externalMarks = extMarks;
    }
    void getData(){
        [Link]("\n\n\n\n" + "Student Deatils ---------" 
+ "\n");
        [Link]("Student Name : " + name + "\n" + "Roll N
umber : " + roll + "\n" + "Subject Name : " + subject + "\n" 
+ "Subject Code : " + subjectCode + "\n" + "Internal Marks : 
" + internalMarks + "\n" + "External Marks : " + externalMar
ks);
    }
}
public class Marksheet {
    public static void main(String []args){
        Scanner sc = new Scanner([Link]);
        Child c1 = new Child();
        [Link]("Enter name : ");
        String sname = [Link]();
        [Link]("Enter Roll No. : ");
        int sroll = [Link]();
        [Link]("Enter Subject Name : ");
        String subjectName = [Link]();
        [Link]("Enter Subject Code : ");
        String scode = [Link]();
        [Link]("Enter Internal Marks : ");
        int imarks = [Link]();
        [Link]("Enter External Marks : ");
        int emarks = [Link]();
        [Link](sname, sroll, subjectName, scode, imarks, emarks)
;
        [Link]();
    }
} //for output see Fig. 2.1

Output:-

Fig. 2.1

Ques 2 - Develop a program using function overloading for adding two


given integer matrices, two floating point number matrices and
double precision value matrices separately.
Ans 2 - import [Link];
class Matrix {
    void addMatrix(double[][] a, double[][] b) {
        double[][] sum = new double[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                sum[i][j] = a[i][j] + b[i][j];
            }
        }
        [Link]("\nFirst Double matrix is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](a[i][j] + "     ");
            }
            [Link]();
        }

        [Link]("\nSecond Double matrix is : ")
;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](b[i][j] + "     ");
            }
            [Link]();
        }

        [Link]("\nThe addition of two Double m
atrices is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](sum[i][j] + "     ");
            }
            [Link]();
        }
    }
    void addMatrix(int[][] a, int[][] b) {
        int[][] sum = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                sum[i][j] = a[i][j] + b[i][j];
            }
        }
        [Link]("\nFirst Integer matrix is : ")
;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](a[i][j] + "     ");
            }
            [Link]();
        }

        [Link]("\nSecond Integer matrix is : "
);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](b[i][j] + "     ");
            }
            [Link]();
        }

        [Link]("\nThe addition of two Integer 
matrices is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](sum[i][j] + "     ");
            }
            [Link]();
        }
    }
    void addMatrix(float[][] a, float[][] b) {
        float[][] sum = new float[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                sum[i][j] = a[i][j] + b[i][j];
            }
        }
        [Link]("\nFirst Floating type matrix i
s : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](a[i][j] + "     ");
            }
            [Link]();
        }

        [Link]("\nSecond Floating type matrix 
is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](b[i][j] + "     ");
            }
            [Link]();
        }
        [Link]("\nThe addition of the Floatin
g type matrices is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                [Link](sum[i][j] + "     ");
            }
            [Link]();
        }
    }
}

public class AddMatrix {
    public static void main(String[] args) {
        // Scanner sc = new Scanner([Link]);
        // double[][] x = new double[3][3];
        // double[][] y = new double[3][3];

        double x[][]={{4.5,3,4},{2.5,7.4,3.1},{5,4.8,5}};  
  
        double y[][]={{7,3.1,4.99},{2.1,4.5,3},
{0.11,2,4.22,4.1}};

        int a[][]={{4,3,4},{2,7,3},{5,4,5}};    
        int b[][]={{7,3,4},{2,4,3},{8,2,4}};

        float c[][]={{4.1f,3.8f,4},{2.2f,7.11f,3.96f},
{5.5f,4.1f,5.11f}};    
        float d[][]={{7f,3.5f,0.52f},{2.1f,0.5f,3.21f},
{8.52f,2.21f,4.5f}};

        // [Link]("\nEnter the elements of fir
st 3X3 matrix :");
        // for (int i = 0; i < 3; i++) {
        //     for (int j = 0; j < 3; j++) {
        //         x[i][j] = [Link]();
        //     }
        // }
        // [Link]("\nEnter the elements of sec
ond 3X3 matrix :");
        // for (int i = 0; i < 3; i++) {
        //     for (int j = 0; j < 3; j++) {
        //         y[i][j] = [Link]();
        //     }
        // }
        Matrix m = new Matrix();
        [Link](x, y);
        [Link](a, b);
        [Link](c, d);
    }
}

//for output see Fig 2.2

Output:-
Fig. 2.2
Ques 3 - Write the Output with description of the following :
class A{
static{
[Link](“Static Block”);
}
static public void main(String...args){
[Link](“This is the main block”);
}
}
Ans 3 - Above given program print “Static Block” first and then print “This is
main block”.
Because “Static Block” has written under the static block and in Java,
static block executes first because it has not required object to invoke
static block. And The static block holds the memory itself and does
not require any variable.

Output:-

Fig. 2.2

Common questions

Powered by AI

Inheritance and method overloading complement each other by promoting code reusability and extensibility. Inheritance allows classes to use or override methods and properties from parent classes, enabling hierarchical organization and the ability to build upon existing code bases. Method overloading further extends this by allowing actions to occur based on differing parameter lists, enabling polymorphic behavior within classes. This combination is powerful for scalable applications because it allows subclasses to adapt inherited methods to suit specific needs while maintaining cohesion and reducing redundancy across the code base .

The mark sheet program utilizes object-oriented principles by creating a base class and a derived class. The base class 'Base' includes fundamental attributes such as the student's name, roll number, and subject name. This provides a generic template that can be extended or specialized. The derived class 'Child', which inherits from 'Base', adds specific attributes like subject code, internal marks, and external marks. This structure allows better data organization and encapsulation, promoting modularity and reusability .

The use of a fixed matrix size in the function overloading example limits the flexibility of the program to handle only 3x3 matrices, which is a constraint in scenarios requiring operations on varied or dynamic matrix dimensions. This restricts the program's applicability in real-world problems that require dynamic input sizes. The advantage of fixed size is simplification in method implementation; however, it undermines scalability and adaptability. For broader applicability, dynamic handling through resizing or parameter-based sizing would be necessary, though it would introduce additional complexity related to memory management and bound checking .

Using static blocks in Java programs can lead to several pitfalls. If the static block contains complex code or operations reliant on external systems (e.g., reading file configurations), it can become a single point of failure during class loading, potentially causing the program to crash or hang. Another issue is that static blocks can't be easily bypassed, creating issues in test environments where setup might need to be different or entirely skipped. They also reduce flexibility, as static block code cannot be varied at runtime and conditionally executed based on different user inputs or environmental conditions .

In the process described in the university mark sheet program, input is read using the Scanner class to capture various details from the user such as student name, roll number, subject name, etc. These inputs are passed as arguments to a method in the derived class 'Child', which sets the values of its data members using these arguments. The object construction is completed once all required data is gathered and stored, allowing the details to be printed or processed further. This method encapsulates data initialization and object setup within a constructor or setup method, providing an organized approach to object creation .

Encapsulating matrix operations within a class enhances the design and usability by promoting information hiding, where internal states such as the matrix data structure are protected from unauthorized external access. This allows for the safe modification of the data representation and operation algorithms without impacting code that relies on these implementations. The program becomes more modular, making the logic reusable across applications and easier to maintain or upgrade. It reinforces adherence to single-responsibility principles by separating concerns related to matrix management from other functionalities .

In Java, static blocks are executed first, followed by the main method, when a class is loaded. This is because static blocks do not require an object instantiation to be executed, they are executed once when the class loading process happens. In the given example, the program prints 'Static Block' first from the static initialization block and then 'This is the main block' from the main method. Static blocks are often used for initialization that should occur before any other processing takes place in the class .

Static blocks in Java are advantageous for performing class-level initialization logic that must be executed once at the time of class loading. They allow initializing static variables or performing any setup required before the class is used, without relying on object instantiation, leading to more efficient resource management. This can also safely handle shared resources or configurations needed by the class, ensuring they are set up once globally. They provide predictability and consistency, vital in large applications where the sequence of setup operations impacts function .

Function overloading is used to perform matrix addition for different data types (integers, floating-point numbers, and double precision values) by defining multiple methods with the same name 'addMatrix' but different parameter types. This allows the same functionality (matrix addition) to be applied efficiently across different data types, promoting code reusability and readability while reducing errors from type-specific functions. It simplifies the interaction of classes with different data types .

The Scanner class in Java offers the benefit of easy parsing of primitive types and strings from the input stream, providing a straightforward and human-readable way of gathering input from users, as seen in the mark sheet example. However, potential issues include its blocking behavior, where the program waits for user input, potentially causing delays. Additionally, Scanner is not thread-safe, which could be a problem in concurrent applications. It can also lead to resource management challenges since it opens a stream and must be closed properly to avoid memory leaks .

You might also like