This question already has answers here:
What are Generics in Java? [closed]
(3 answers)
Closed 3 years ago.
im having a problem calling a method i created for a class when it is returned from a list. I get a "java.lang.Object cannot be converted to Thing"
error when running the following code
public class Test1
{
public static void main(String args[])
{
Thing whipersnaper = new Thing(30, "whipersnaper");
List storage = new List();
storage.insert(whipersnaper);
Thing x = storage.getItem(0);
x.doubleWeight();
System.out.println(x.getWeight());
}
}
here is the "Thing" class
public class Thing
{
private int weight;
private String name;
public Thing(int weight,String name){
this.weight = weight;
this.name = name;
}
public void doubleWeight(){
this.weight *= 2;
}
public int getWeight(){
return this.weight;
}
}
finally here is the List class
public class List
{
private Object[] itemList;
private int size;
public List()
{
this.itemList = new Object[10];
this.size = 0;
}
public void insert(Object item){
itemList[size] = item;
size++;
}
public Object getItem(int index){
return itemList[index];
}
}
i need the list to be able to hold objects of any type and not exclusively Thing objects.
i have tried to google a solution but I cant find a good way to phrase the question to get an answer. thanks in advance.
Change that line Thing x = storage.getItem(0); with Thing x = (Thing) storage.getItem(0);
Thing x = (Thing) storage.getItem(0);
This question already has answers here:
Java Reserve Keywords
(4 answers)
Closed 7 years ago.
i'm writing the following code and i'm getting the error 'identifier expected' on the line
private String class;
public class Records
{
private String name;
private String class;
private int cabinNumber;
public Records(String n, String c, int cn)
{
name = n;
class = c;
cabinNumber = cn;
}
public void setClass (String c){
class = c;
}
public void set cabinNumber (int cn){
cabinNumber = cn;
}
public String name(){
return name;
}
public String class(){
return class;
}
public int cabinNumber(){
return cabinNumber;
}
}
can someone please explain why this is happening and what I can do to fix it?
thank you!
class is a java keyword, you cannot have a variable with this name.
public class StuTest2
{
public static final int NUMBER_OF_STUDENTS = 7;
public static void main(String[] args)
{
Student[] stus = new Student[NUMBER_OF_STUDENTS];
// Student has ID, name, and GPA
stus[0] = new Student(123, "Suzy", 3.9);
// Default for missing GPA will be 9.99 "special value
stus[1] = new Student(234, "Tom");
// Default name will be "Student #xxx" where
// "xxx" is the actual ID number
stus[2] = new Student(456);
// A grad student also has a thesis topic
stus[3] = new GradStudent(567, "Fred", 3.8, "Java");
// Default thesis topic is "Undecided"
stus[4] = new GradStudent(678, "Staci", 3.1);
// Doctoral students earn a stipend
stus[5] = new DoctoralStudent(789, "Mandy", 4.0, "Databases", 3550.00);
// If missing, the default stipend is $3000.00
stus[6] = new DoctoralStudent(890, "Ned", 3.7, "Cisco Networking");
// Inside the loop, the toString method is called for each
// student. All graduate students show the word "Graduate" in
// front of the output from this method.
for(Student stu : stus)
{
}
}
}
class Student
{
private int id;
private String name;
private double gpa;
public Student(int i, String n, double g)
{
id = i;
name = n;
gpa = g;
}
public Student(int i)
{
this(i, "Student #" + i);
}
public Student(int i, String n)
{
this(i, n, 9.99);
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public double getGPA()
{
return gpa;
}
public String toString()
{
return System.out.println(stus.getId+", " + stus.getName
+ ", " + stus.getGPA);
}
}
class GradStudent extends Student
{
private String topic;
public GradStudent(int i, String n, double g, String t)
{
super(i, n, g);
topic = t;
}
public GradStudent(int i, String n, double g)
{
this(i, n, g, "Undecided");
}
public String getTopic()
{
return topic;
}
public String toString()
{
return super.getTopic();
}
}
class DoctoralStudent extends GradStudent
{
private double stip;
public DoctoralStudent(int i, String n, double g, String t, double s)
{
super(i, n, g, t);
stip = s;
}
public DoctoralStudent(int i, String n, double g, String t)
{
this(i, n, g, t, 3000.00);
}
public double getStip()
{
return stip;
}
public String toString()
{
return super.getStip();
}
}
I'm trying to print out while using the return super.toString(), but Iget errors saying cannot find symbol for stus, but I have it right before starting the student class. What gives? ps, sorry for the bad closings, trying to meet standards on here lol
Your "stus" variable is only in scope inside the main() method, so you can't access it outside of that method. Furthermore, "stus" is an array, so it doesn't even make sense to call getId on it. Further, notice that getId refers to a variable since it doesn't have parenthesis after it.
Keep in mind that in your toString() method, you're already "inside" a Student Object, so you can just call the getId() function directly:
public String toString()
{
return getId() +", " + getName() + ", " + getGPA();
}
Also note that I've removed the System.out.println() function in your toString method, since it doesn't return anything and therefore doesn't make sense to return anyway.
You've got a lot of incorrect syntax in your code, and I highly recommend starting much smaller. You'll have much better luck if you develop your program incrementally instead of trying to do the whole thing in one shot. I recommend starting over and compiling and testing with every single line you add.
This question already has answers here:
What is the point of setters and getters in java? [duplicate]
(14 answers)
Getter-Setter and private variables [duplicate]
(5 answers)
Closed 8 years ago.
public class Addition
{
private int number1,number2;
public void setNumber1()
{
}
public int getNumber1()
{
}
public void setNumber2()
{
}
public int getNumber2()
{
}
}
what is point of keeping variables private if i can access them using public getter and setter method.
Having a setter method allows you to implement logic prior to assigning a value to the private variable. This prevents other code from assigning arbitrary values to your private variable which might cause your object to be in an invalid state. For example:
Without setter method
public class Man {
public int height;
}
//...some code
man.height = -1; // BOOOM!
With setter method:
public class Man {
private int height;
public void setHeight(int h) {
this.height = h >= 0 ? h : 0;
}
}
//...
man.setHeight(-10); // Will keep the man in valid state
You can add a validation in setters.
private int age;
public void setAge(int a){
if(a>0){
this.age = a;
}else{
this.age = 0;
}
}
You can assume that making a variable as private is a basic guideline for coding. If you make them public it is accessible for outside world and any one can modify it.
Suppose that number1 should always be +ve int in your case. So the setter method will have check and help you to avoid setting -ve values to it. See below:
public void setNumber1(int number)
{
if(number >= 0)
{
this.number1 = number
}
else
{
//you can throw exception here
}
}
It follows a important Object Oriented Property Encapsulation .
For example I have a integer variable price with public modifier(Any one can access it)
public int price;
now we know that price can not negative but it is public so we don't have any control in this. Now see it with respect to encapsulation
private int price;
public void setPrice(int price)
{
if(price>0)
this.price=price
}
Here we have control, no one can set negative value of price. This is the power of Encapsulation "Giving Control".
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 years ago.
Keep getting this error, sorry I am a beginner in Java.
Exception in thread "main": java.lang.NullPointerException
at assignment01.Student.addGrade(Student.java:28)
at assignment01.GpaTest.main(GpaTest.java:11)
package assignment01;
public class Grades
{
private double qualPts;
private int numCred;
public double getGPA()
{
if(numCred!=0)
{
return(qualPts/numCred);
}
return numCred;
}
public void addGrade(int creds, double grade)
{
grade+=creds+numCred;
qualPts+=creds*grade;
}
public int getNumCred()
{
return numCred;
}
}
.
package assignment01;
public class Student
{
private String name;
private String bNumber;
private Grades grades;
public Student(String name, String bNumber)
{
this.name=name;
this.bNumber=bNumber;
}
public void addGrade(int creds, double grade)
{
grades.addGrade(creds, grade);
}
.
package assignment01;
public class GpaTest {
public static void main(String[] args)
{
Student theStudent= new Student("Ethan","00000000");
int CREDITS_ENROLLED1=4;
double GRADE1=90;
theStudent.addGrade(1, 100);
theStudent.addGrade(CREDITS_ENROLLED1,GRADE1);
System.out.println("Determining the grades of student named Ethan.");
System.out.println("Ethan has a grade of 90.");
}
}
grades variable is not initialized. You need to initialize it inside Student constructor:
public Student(String name, String bNumber)
{
this.name=name;
this.bNumber=bNumber;
this.grades = new Grades();
}
You may wish to initialize qualPts and numCred as well
Initialize the object 'grades'.