I got to assignment first one was to print the data of two students using different classes and constructors and count the total numbers of records and i've done this.
here is the program.
public class Student
{
int RollNo;
String name;
static int stdcount=0;
public void print(){
System.out.println ("Roll no: " + RollNo + "" + " Name: " + name);
}
public Student (){
RollNo= 99999;
name = "Name is not set";
stdcount++;
}
}
class Records
{
public static void main (String args[])
{
Student std1 = new Student();
Student std2 = new Student();
std1.RollNo = 2000;
std1.name = "Ghouri";
std2.RollNo = 200;
std2.name = "Ghori";
std1.print();
std2.print();
System.out.println ("Total Students: " + Student.stdcount);
}
}
in the above code, in line 5 when i m not writing static with the stdcount=0 then program is giving an error,
Student.java:32: non-static variable stdcount cannot be referenced from a static
context
System.out.println ("Total Students: " + Student.stdcount);
this is because i am referencing stdcount in a static context, but int RollNo and String name are also not static then why program is working without writing static with them ???
The second question of the Assignment is,
Calculate volume of a cube, area of a square, parameters of square and rectangle, area of a circle using constructors and different classes, set the value to 1 and define other constructors that can be call to set values of data other than 1. show the count of total number of objects and total number of general objects.
but i didn't understand the question and don't know what to do in this question :(