Hey, Hi all!
I'm a bachelor student, new to Java.
Following code is giving error in PrintDetails()
What parameters should I define in it??
Kindly check and tag out my mistakes in the following code.. Thanks ^_^

class student
{
String a;String b;char c;double d;
student(String name,String rollno,char gender,double gpa)
{
    a= name; 
    b= rollno;
    c= gender; 
    d= gpa;
}}
class teacher
{
String e;String f;long g;String h;
teacher(String name,String address,long telephoneno,String degree)
{
    e=name;
    f=address;
    g=telephoneno;
    h=degree;
}}
class course
{
    student s1;
    student s2;
    teacher t1;
    int age;
course(student x,student y,teacher z)
{
    s1=x;
    s2=y;
    t1=z;
}
void printDetails()
{
System.out.println();
}}
public class test1
{
public static void main(String args[])
{
    student name=new student();
    course c1=new course("aaaa","11cs11",'f',6.8);
    course c2=new course("uuu","8u8",'p',7.9);
    course c3=new course("uuuttt","99oi88",'m',9.7);
    course j[]={c1,c2,c3};
    c1. printDetails();
    c2.printDetails();
    c3.printDetails();
}}

Dani AI

Generated

The compiler errors come from mismatched constructor usage rather than the printDetails() signature. asked for the error text, but the code shows two common mistakes: calling constructors with the wrong parameter lists (for example calling new student() when no no-arg constructor exists), and passing primitive/string/gpa values to the Course constructor that expects Student and Teacher objects. printDetails() usually does not need parameters if it prints the instance fields of the Course object — create the Student and Teacher objects first, then pass them into the Course constructor.

Example of a corrected approach (uses clearer names and a parameterless printDetails()):

class Student {
  String name;
  String roll;
  char gender;
  double gpa;
  Student(String name, String roll, char gender, double gpa) {
    this.name = name;
    this.roll = roll;
    this.gender = gender;
    this.gpa = gpa;
  }
  String summary() { return name + " (" + roll + ") gpa=" + gpa; }
}

class Teacher {
  String name;
  String addr;
  long phone;
  String degree;
  Teacher(String name, String addr, long phone, String degree) {
    this.name = name; this.addr = addr; this.phone = phone; this.degree = degree;
  }
}

class Course {
  Student s1, s2;
  Teacher teacher;
  Course(Student s1, Student s2, Teacher teacher) { this.s1 = s1; this.s2 = s2; this.teacher = teacher; }
  void printDetails() {
    System.out.println(s1.summary());
    System.out.println(s2.summary());
    System.out.println("Teacher: " + teacher.name + ", " + teacher.degree);
  }
}

Key tips and checks:

  • Either call existing parameterized constructors with correct args or add a no-arg constructor if you really need new Student().
  • When creating a Course, pass Student/Teacher objects (or change the Course constructor to accept primitives if that was intended).
  • Use 1234567890L for long literals (phone numbers) and single quotes for char.
  • After fixing construction, printDetails() can remain parameterless and read the instance fields; that is the usual and simplest design.

Recommended Answers

All 2 Replies

code is giving error

Please post the full text of the error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.