This is my student class.
public class Student {
private String name;
private long id;
private double gpa;
private PersonalData pd;
public Student(String name, long id, double gpa, PersonalData pd){
this.name= name;
this.id=id;
this.gpa= gpa;
this.pd= pd;
}
public String getName(){
return name;
}
public long getID(){
return id;
}
public double getGPA(){
return gpa;
}
public PersonalData getPersonalData(){
return pd;
}
public String toString(){
String result= "Name: "+ name+ ", ID: "+ id+ ", GPA: "+gpa+", Personal Data: "+ pd;
return result;
}
}
Here is the test class:
public class Test {
public static void main(String[] args) {
// TODO, add your application code
PersonalData a= new PersonalData(80,4,1,1232154687);
Student b= new Student(Ali,123,20,a);
}
}
It gives this error:
cannot find symbol
Student b= new Student(Ali,123,20,a);
^
symbol: variable Ali
location: class Test
1 error
I don't know what is wrong. Is there anyone can see?
Make it like below
student b= new Student("Ali",123,20,a);
Your Student class constructor requires String as name either you have to pass it as in "" or Make a String variable like String Ali="" then pass Ali as
student b= new Student(Ali,123,20,a);
Related
i have written this java code and i get an error can anyone tell me why? the code is to create a student class and will be tested.
i am new to java so any help will be appreciated
this is the code:
import java.io.*;
import java.util.*;
public class Student {
private static void main(String[] args)
{
private String forName;
private String surName;
private String studentID;
private String degreeScheme;
//This is the Constructor of the
class Student
public Student(String name) {
this.forName = forName;
}
//Assign the surname of the student
public void stuSurname (String
stuSurname){
surName = stuSurname;
}
//Assign the student ID to the
student
public void stuID (String stuID){
studentID = stuID;
}
//Assign the Degree of the Student
public void stuDegree (String
stuDegree){
degreeScheme = stuDegree;
}
//Print the student details
public void printStudent(){
System.out.println("Forname:"+ forName);
System.out.println("Surename:"+ surName);
System.out.println("Student
ID:"+ studentID);
System.out.println("Degree
Scheme:"+ degreeScheme);
}
// setter
public void setForName(String
forName) {
this.forName = forName;
}
// getter
public String getForName() {
return forName;
}
}
}
and this is the error that i get:
TheRealFawcett:lab8 therealfawcett$ javac
Student.java
Student.java:8: error: illegal start of
expression
private String forName;
^
Student.java:49: error: class, interface, or enum
expected
}
^
2 errors
TheRealFawcett:lab8 therealfawcett$
i don't understand why i get this error as i thought my main method was correct.
Fields belong to the Class and not to to the method. Also methods have to be at class level.
In your code all fields and methods are in the main method, which is not correct.
The following snippet shows a correct version:
public class Student {
private static void main(String[] args) {
Student student = new Student("Charles");
}
private String forName;
private String surName;
private String studentID;
private String degreeScheme;
//This is the Constructor of the
public Student(String name) {
this.forName = forName;
}
//Assign the surname of the student
public void stuSurname(String stuSurname) {
surName = stuSurname;
}
//Assign the student ID to the student
public void stuID(String stuID) {
studentID = stuID;
}
//Assign the Degree of the Student
public void stuDegree(String stuDegree) {
degreeScheme = stuDegree;
}
//Print the student details
public void printStudent() {
System.out.println("Forname:" + forName);
System.out.println("Surename:" + surName);
System.out.println("Student ID:" + studentID);
System.out.println("Degree Scheme:" + degreeScheme);
}
// setter
public void setForName(String forName) {
this.forName = forName;
}
// getter
public String getForName() {
return forName;
}
}
To learn more about the Java Classes and Objects you can follow this official tutorial.
So as I understand, Goat class has no problem with a code,
I want to assign the first two elements from a new object, but I am not sure why it gives an runtime error
class Pet {
public String name;
public boolean indoor;
public Pet(String name, Boolean indoor) {
this.name = name;
this.indoor = indoor;
}
public String toString(){
return name + ", " + indoor;
}
}
class Goat extends Pet {
public int age;
public String diet;
public Goat(String name, boolean indoor, int age, String diet) {
super(name, indoor);
this.age = age;
this.diet = diet;
}
Here is the test code and error
}
The error is being thrown because, Pet class constructor is expecting Boolean and Goat class constructor is passing boolean. Although compiler is compiling code, but at runtime java is not able to find boolean type constructor. Hence, throwing no method error.
Correct Code will be
class Pet {
public String name;
public boolean indoor;
public Pet(String name, boolean indoor) {
this.name = name;
this.indoor = indoor;
}
public String toString(){
return name + ", " + indoor;
}
}
class Goat extends Pet {
public int age;
public String diet;
public Goat(String name, boolean indoor, int age, String diet) {
super(name, indoor);
this.age = age;
this.diet = diet;
}
I am having errors below and I am confused why this is a Lab to make a storage spot for a students information.
import java.util.Arrays;
I have a problem with the square brackets below
public class Student {
private String studentID;
private String studentName;
private String studentMajor;
private double studentGPA;
private String studentGrad;
private long[] ;
And a problem with Student() below
public Student(){
studentID = "";
studentName = "";
studentMajor = "";
studentGPA = 0;
studentGrad = "";
}
public Student(String stuID, String stuName, String stuMajor, double stuGPA, String stuGrad) {
studentID = stuID;
studentName = stuName;
studentMajor = stuMajor;
studentGPA = stuGPA;
studentGrad = stuGrad;
}
public void setID(String ID) {
studentID = ID;
}
public void setName(String name) {
studentName = name;
}
public void setMajor(String major) {
studentMajor = major;
}
public void setGPA(double GPA) {
studentGPA = GPA;
}
public void setGrad(String Grad) {
studentGrad = Grad;
}
public String getID() {
return studentID;
}
public String getName() {
return studentName;
}
public String getMajor() {
return studentMajor;
}
public double getGPA() {
return studentGPA;
}
public String getGrad() {
return studentGrad;
}
public void printData() {
System.out.println("Student ID: "+studentID);
System.out.println("Student Name: " +studentName );
System.out.println("Student Major: "+studentMajor);
System.out.println("Student GPA: "+ studentGPA);
System.out.println("Student Year of Graduation: " +studentGrad);
}
}
private long[]; is incorrect syntax.
To complete the variable you must insert an identifier (the variable name).
In most IDE's (i.e. Eclipse and Netbeans), this incomplete variable will cause the next line to have issues. This is why public Student(){ (despite being completely valid code) is giving you an error. If you temporarily delete the line private long[]; you should see this error go away.
From the code you supplied, it looks like private long[]; can be deleted anyway as an array isn't being used.
public class StudentTest{
public static void main(String args[]){
UnderGrad uG1 = new UnderGrad();
uG1.setName("John");
uG1.setMatric("0192345");
System.out.println("Undergarduate Student Info");
uG1.setCourse(new Course("CS1103",3));
uG1.setCourse(new Course("IT4504",3));
uG1.displayStudentInfo();
System.out.println(" ");
PostGrad pG1 = new PostGrad();
pG1.setName("Sam");
pG1.setMatric("G015466");
pG1.setResearch("Empirical Software Engineering");
pG1.setResearch("Data Mining");
System.out.println("Postgrad Student Info");
pG1.displayStudentInfo();
}
}
public class Course{
private String courseName;
private int crhour;
public Course(String n, int c){
courseName = n;
crhour = c;
}
public void setCourseName(String course){
courseName = course;
}
public String getCourseName(){
return courseName;
}
public void setCreditH(int c){
crhour = c;
}
}
public class Student{
private String matric ="-matric required-";
private String name="-name required-";
public Student(){
}
public void setName(String n){
if (n.matches("[a-zA-Z]+") == false)
System.out.println("Invalid Name");
else
name = n;
}
public String getName(){
return name;}
public void setMatric(String m){
matric = m;}
public String getMatric(){
return matric;}
}
public class UnderGrad extends Student{
private Course courseList[];
private int index = 0;
public UnderGrad(){
Course courseList[] =new Course[7];}
public void setCourse(Course courseName){
//Course courseList[]= new Course[2];
}
public Course[] getCourse(){
return courseList;}
public void displayStudentInfo(){
System.out.println("Name: "+getName());
System.out.println("Matric: "+getMatric());
System.out.println("Course List: "+getCourse());
}}
public class PostGrad extends Student{
private String researchArea[];
private int index = 0;
public PostGrad()
{
researchArea = new String[5];
}
public void setResearch(String research){
for(index=0;index<2;index++){
researchArea[index]=research;}
}
public String[] getResearch(){
return researchArea;}
public void displayStudentInfo(){
System.out.println("Name: "+getName());
System.out.println("Matric: "+getMatric());
System.out.println("Research List: "+getResearch());
}}
Output:
Undergarduate Student Info
Name: John
Matric: 0192345
Course List: null
Postgrad Student Info
Name: Sam
Matric: G015466
Course List: [Ljava.lang.String;#2ac9fefa
The problem I cant get the value of String of the course an the research. What should I do?Should I use super reference?
Here:
System.out.println("Course List: "+getCourse());
You're printing out the default toString() returned by an array of String. Don't do that. Iterate through the array and print each item or else use java.util.Arrays.toString(...).
System.out.println("Course List: "+ java.util.Arrays.toString(getCourse()));
You will also need to give your Course class a valid toString() method, one that returns the courseName and perhaps the credit hours. Also I would change the course field to courses or courseList to reflect that it does not represent one single course but rather a collection of courses. Likewise the getter method should reflect the field name change.
I was told that when creating a new object, it needs to have the same parameters as its constructor. Ive tried, but i still get these error. cannot find symbol s1.getCourse s1.getName s1.getAge. and also an invalid constructor error. heres my code
public class Person{
private String name;
private int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
public String getDetails(){
return "Name: " + name + "Age: " + age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public class Student extends Person{
private String course;
public Student(String name, int age,String course){
super(name,age);
this.course = course;
}
public String getDetails(){
return super.getDetails()+"Course: "+ course;
}
public void setCourse(String course){
this.course=course;
}
public String getCourse(){
return course;
}
public String getName(){
return name;
}
}
public String getAge(){
return age;
}
}
TestPerson
student++;
String name=array[0];
int age=Integer.parseInt(array[1]);
String course=array[3];
Student s1= new Student(name,age,course);
System.out.println("name "+s1.getName());
System.out.println("age "+ s1.getAge());
System.out.println("course: " + s1.getCourse());
}
}
For more Detail can u provide Person class.
You have not provided the name and age attribute in student class.
if all this is present in super class then also When u call the s1.getName() method it will call from student class. because it present in sub class i.e student