Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Java SYMarks Packages, Compiling and Code

coding java

Uploaded by

sanjuwagh2909
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Java SYMarks Packages, Compiling and Code

coding java

Uploaded by

sanjuwagh2909
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q.

2 Write a Java program to create a Package “SY” which has a class SYMarks (members –
ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package TY which has a
class
TYMarks (members – Theory, Practicals). Create n objects of Student class (having
rollNumber,
name, SYMarks and TYMarks). Add the marks of SY and TY computer subjects and
calculate
the Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50 , Pass Class for > =40 else ‘FAIL’) and
display the result of the student in proper format.
What do you want to do with the text?
You
Sent by you:
java program and then sample example
Copilot
Generating answer using selected text
Sent by Copilot:

Sure! Here’s a Java program that fulfills the requirements of the given problem statement.
The program creates two packages, SY and TY, with the respective
classes SYMarks and TYMarks. It then creates a Student class to manage student details and
calculate grades.

Step-by-Step Solution

1. Create the SY package with the SYMarks class:

Java
// File: SY/SYMarks.java
package SY;

public class SYMarks {


public int ComputerTotal;
public int MathsTotal;
public int ElectronicsTotal;

public SYMarks(int computerTotal, int mathsTotal, int electronicsTotal)


{
this.ComputerTotal = computerTotal;
this.MathsTotal = mathsTotal;
this.ElectronicsTotal = electronicsTotal;
}

@Override
public String toString() {
return ComputerTotal + "\t" + MathsTotal + "\t" + ElectronicsTotal;
}
}
AI-generated code. Review and use carefully. More info on FAQ.
2. Create the TY package with the TYMarks class:

Java
// File: TY/TYMarks.java
package TY;

public class TYMarks {


public int Theory;
public int Practicals;

public TYMarks(int theory, int practicals) {


this.Theory = theory;
this.Practicals = practicals;
}

@Override
public String toString() {
return Theory + "\t" + Practicals;
}
}
AI-generated code. Review and use carefully. More info on FAQ.

3. Create the Student class to manage student details and calculate grades:

Java
// File: Student.java
import SY.SYMarks;
import TY.TYMarks;
import java.util.Scanner;

public class Student {


int rollNumber;
String name;
SYMarks syMarks;
TYMarks tyMarks;

public Student(int rollNumber, String name, SYMarks syMarks, TYMarks


tyMarks) {
this.rollNumber = rollNumber;
this.name = name;
this.syMarks = syMarks;
this.tyMarks = tyMarks;
}

public void calculateGrade() {


int totalMarks = syMarks.ComputerTotal + tyMarks.Theory +
tyMarks.Practicals;
double percentage = totalMarks / 3.0;
String grade;
if (percentage >= 70) {
grade = "A";
} else if (percentage >= 60) {
grade = "B";
} else if (percentage >= 50) {
grade = "C";
} else if (percentage >= 40) {
grade = "Pass";
} else {
grade = "FAIL";
}

System.out.println("Grade: " + grade);


}

@Override
public String toString() {
return rollNumber + "\t" + name + "\t" + syMarks + "\t" + tyMarks;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = scanner.nextInt();
Student[] students = new Student[n];

for (int i = 0; i < n; i++) {


System.out.println("Enter details for student " + (i + 1) +
":");
System.out.print("Roll Number: ");
int rollNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("SY Marks (Computer, Maths, Electronics): ");
int computerTotal = scanner.nextInt();
int mathsTotal = scanner.nextInt();
int electronicsTotal = scanner.nextInt();
SYMarks syMarks = new SYMarks(computerTotal, mathsTotal,
electronicsTotal);
System.out.print("TY Marks (Theory, Practicals): ");
int theory = scanner.nextInt();
int practicals = scanner.nextInt();
TYMarks tyMarks = new TYMarks(theory, practicals);

students[i] = new Student(rollNumber, name, syMarks, tyMarks);


}
System.out.println("\nStudent Details:");
System.out.println("RollNo\tName\tSYMarks\t\tTYMarks\t\tGrade");
for (Student student : students) {
System.out.print(student + "\t");
student.calculateGrade();
}

scanner.close();
}
}
AI-generated code. Review and use carefully. More info on FAQ.

Explanation

 Packages and Classes: The program defines two packages, SY and TY, each containing a
class (SYMarks and TYMarks respectively) to hold the marks.
 Student Class: The Student class contains fields for roll number, name, and marks from
both SY and TY. It includes methods to calculate the grade based on the marks and to display
student details.
 Main Method: The main method accepts input for multiple students,
creates Student objects, and displays their details along with calculated grades.

Feel free to ask if you have any questions or need further assistance!
Learn more
1github.com2github.com3onecompiler.com4github.com
You
Sent by you:
please give me sample output for given java code
Copilot
Usingthe web
Sent by Copilot:

Sure! Here’s a sample output for the given Java code. Let’s assume we have 2 students with
the following details:
1. Student 1:
o Roll Number: 1
o Name: Alice
o SY Marks: Computer: 75, Maths: 80, Electronics: 70
o TY Marks: Theory: 65, Practicals: 70
2. Student 2:
o Roll Number: 2
o Name: Bob
o SY Marks: Computer: 55, Maths: 60, Electronics: 50
o TY Marks: Theory: 45, Practicals: 50

Sample Output
Enter number of students: 2
Enter details for student 1:
Roll Number: 1
Name: Alice
SY Marks (Computer, Maths, Electronics): 75 80 70
TY Marks (Theory, Practicals): 65 70

Enter details for student 2:


Roll Number: 2
Name: Bob
SY Marks (Computer, Maths, Electronics): 55 60 50
TY Marks (Theory, Practicals): 45 50

Student Details:
RollNo Name SYMarks TYMarks Grade
1 Alice 75 80 70 65 70 A
2 Bob 55 60 50 45 50 C

Explanation

 Student 1: The total marks for computer-related subjects are (75 (SY) + 65 (Theory) + 70
(Practicals) = 210). The average is (210 / 3 = 70), which corresponds to grade ‘A’.
 Student 2: The total marks for computer-related subjects are (55 (SY) + 45 (Theory) + 50
(Practicals) = 150). The average is (150 / 3 = 50), which corresponds to grade ‘C’.

Feel free to modify the input values and run the program to see different outputs! If you have
any more questions or need further assistance, just let me know.

To compile and run the Java program with packages, follow these steps:

Step-by-Step Compilation and Execution


1. Create Directory Structure:
o Create a directory structure that matches the package names. For example:
o project/
o ├── SY/
o │ └── SYMarks.java
o ├── TY/
o │ └── TYMarks.java
o └── Student.java
2. Write the Java Files:
o Save the SYMarks.java file in the SY directory.
o Save the TYMarks.java file in the TY directory.
o Save the Student.java file in the project directory.
3. Compile the Java Files:
o Open a terminal or command prompt.
o Navigate to the project directory.
o Compile the SYMarks.java file:
o javac SY/SYMarks.java
o Compile the TYMarks.java file:
o javac TY/TYMarks.java
o Compile the Student.java file:
o javac -cp . Student.java
4. Run the Program:
o Run the Student class:
o java -cp . Student

Example
Here’s how it looks in the terminal:
$ cd project
$ javac SY/SYMarks.java
$ javac TY/TYMarks.java
$ javac -cp . Student.java
$ java -cp . Student

Output
After running the program, you will be prompted to enter the number of students
and their details. The program will then display the student details along with their
grades.

If you follow these steps, you should be able to compile and run the program
successfully. Let me know if you encounter any issues or have further questions!

You might also like