I am trying to build a program which counts each student from 1 - 10. But the output seems to skip 5 after 4 and go straight to 6.
The output I get is:
MathStudent[1]-Smith: - Count:1
MathStudent[2]-Jack: - Count:1
MathStudent[3]-Victor: - Count:1
MathStudent[4]-Mike: - Count:1
Science Student[6]-Dave: - Count:1
Science Student[7]-Oscar: - Count:1
Science Student[8]-Peter: - Count:1
Computer Student[9] Philip: - Count:1
Computer Student[10] Shaun: - Count:1
Computer Student[11] Scott: - Count:1
Main class:
public class JavaLab5 {
public static final int DEBUG = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
StudentThread studentThread = new StudentThread();
studentThread.start();
}
}
Student class:
public class Student {
static int studentCounter = 1;
String name;
private int count = 0;
public static int instances = 0;
// Getters
public String getName() {
return this.name;
}
// Setters
public void setName(String name) {
if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
/**
* Default constructor. Populates name,age and gender
* with defaults
*/
public Student() {
instances++;
this.name = "Not Set";
}
/**
* Constructor with parameters
* #param name String with the name
*/
public Student(String name) {
this.name = name;
}
/**
* Destructor
* #throws Throwable
*/
protected void finalize() throws Throwable {
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
/**
*
*/
public void getCounter() {
for (int j = 0; j < 1; j++) {
count++;
System.out.println(count);
}
}
#Override
public String toString () {
return this.name;
}
public String getSubjects() {
return this.getSubjects();
}
}
ComputerStudent class:
public class ComputerStudent extends Student {
int studCountObj;
/**
* Default constructor
* #fortanGrade
* #adaGrade
*/
public ComputerStudent() {
super();
}
public ComputerStudent(String name) {
super(name);
studentCounter++;
studCountObj=studentCounter;
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" Computer Student" + "[" + studCountObj + "] " + name + ": ");
}
}
MathStudent class:
public class MathStudent extends Student {
int studCountObj;
/**
* Default constructor
* #param name
*/
public MathStudent(String name) {
super(name);
studCountObj=studentCounter;
studentCounter++;
}
public MathStudent() {
super();
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
}
}
ScienceStudent class:
public class ScienceStudent extends Student {
int studCountObj;
/**
* Default constructor
*/
public ScienceStudent() {
super();
}
public ScienceStudent(String name) {
super(name);
studentCounter++;
studCountObj=studentCounter;
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" Science Student" + "[" + studCountObj + "]" + "-" + name + ": ");
}
}
StudentThread class:
public class StudentThread extends Thread {
public void run(){
Student s[] = new Student[10];
s[0] = new MathStudent("Smith");
s[1] = new MathStudent("Jack");
s[2] = new MathStudent("Victor");
s[3] = new MathStudent("Mike");
s[4] = new ScienceStudent("Dave");
s[5] = new ScienceStudent("Oscar");
s[6] = new ScienceStudent("Peter");
s[7] = new ComputerStudent("Philip");
s[8] = new ComputerStudent("Shaun");
s[9] = new ComputerStudent("Scott");
for (int j = 0; j < 10; j++) {
for (Student item : s) {
System.out.print(item.getSubjects() + " - " + "Count:");
item.getCounter();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
}
My question is why is the output skipping 5?
In Science Class:
super(name);
studentCounter++;
studCountObj=studentCounter;
You first increase the studentCounter from 5 to 6 and then assign it to studCountObj. Swap that would print 5.
You have to change this:
MathStudent class:
public class MathStudent extends Student {
int studCountObj;
/**
* Default constructor
* #param name
*/
public MathStudent(String name) {
super(name);
studentCounter++; //First this
studCountObj=studentCounter; //Then this
}
public MathStudent() {
super();
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
}
}
And this:
Student class:
public class Student {
static int studentCounter = 0; //Start with zero
String name;
private int count = 0;
public static int instances = 0;
// Getters
public String getName() {
return this.name;
}
// Setters
public void setName(String name) {
if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
/**
* Default constructor. Populates name,age and gender
* with defaults
*/
public Student() {
instances++;
this.name = "Not Set";
}
/**
* Constructor with parameters
* #param name String with the name
*/
public Student(String name) {
this.name = name;
}
/**
* Destructor
* #throws Throwable
*/
protected void finalize() throws Throwable {
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
/**
*
*/
public void getCounter() {
for (int j = 0; j < 1; j++) {
count++;
System.out.println(count);
}
}
#Override
public String toString () {
return this.name;
}
public String getSubjects() {
return this.getSubjects();
}
}
In the ScienceStudent class and in the Computer student class you already do this, so when you change this pattern in the Mathstudent class you will get your first output, because when the thread goes to ScienceStudent it will add the number 1 again, you skip a number then.
Related
I re-type these code from a book and somehow I got error " Cannot find symbol - class InventoryItem "
import java.util.Scanner;
public class ReturnObject {
public static void main(String[] args) {
InventoryItem item;
item = getData();
System.out.println("Des: " + item.getDescription() + " Unit: " +
item.Units());
}
public static InventoryItem getData() {
String desc;
int units;
Scanner keyboard = new Scanner(System.in);
System.out.print("enter descri: ");
desc = keyboard.nextLine();
System.out.print("number of unit: ");
units = keyboard.nextInt();
return new InventoryItem(desc, units);
}
}
I'm new to java please help
thank you.
I think this should be the InventoryItem you need.
/**
* This class uses three constructors.
*/
public class InventoryItem {
private String description; // Item description
private int units; // Units on-hand
/**
* No-arg constructor
*/
public InventoryItem() {
description = "";
units = 0;
}
/**
* The following constructor accepts a
* String argument that is assigned to the
* description field.
*/
public InventoryItem(String d) {
description = d;
units = 0;
}
/**
* The following constructor accepts a
* String argument that is assigned to the
* description field, and an int argument
* that is assigned to the units field.
*/
public InventoryItem(String d, int u) {
description = d;
units = u;
}
/**
* The setDescription method assigns its
* argument to the description field.
*/
public void setDescription(String d) {
description = d;
}
/**
* The setUnits method assigns its argument
* to the units field.
*/
public void setUnits(int u) {
units = u;
}
/**
* The getDescription method returns the
* value in the description field.
*/
public String getDescription() {
return description;
}
/**
* The getUnits method returns the value in
* the units field.
*/
public int getUnits() {
return units;
}
}
complete example click here and here
The class you are currently in cannot find the class (symbol) InventoryItem. You need to define this class & the getData method.
public class InventoryItem{
private String desc;
private int units;
public InventoryItem(){
Scanner keyboard = new Scanner(System.in);
System.out.print("enter descri: ");
desc = keyboard.nextLine();
System.out.print("number of unit: ");
units = keyboard.nextInt();
}
public static InventoryItem getData() {
return this;
}
}
maybe your InventoryItemclass:
public class InventoryItem {
private String desc;
private int units;
public InventoryItem(String desc, int units) {
this.desc=desc;
this.units=units;
}
public String getDescription() {
return desc;
}
public int Units() {
return units;
}
}
I am trying to create an array of students which will contain 3 different types of students and each of the students will have 3 variables name, and 2 grades.
This is what I have done so far, and it gives me the following error cannot find symbol.
Main class:
public class JavaLab5 {
public static final int DEBUG = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Student s[] = new Student[10];
s[0] = new MathStudent(Smith,14,15);
s[1] = new MathStudent(Jack,16,19);
s[2] = new MathStudent(Victor,18,21);
s[3] = new MathStudent(Mike,23,28);
s[4] = new ScienceStudent(Dave,32,25);
s[5] = new ScienceStudent(Oscar,28,56);
s[6] = new ScienceStudent(Peter,29,28);
s[7] = new ComputerStudent(Philip,25,38);
s[8] = new ComputerStudent(Shaun,34,39);
s[9] = new ComputerStudent(Scott,45,56);
for (int loop = 0; loop < 10; loop++) {
System.out.println(s[loop].getSubjects());
System.out.print(loop + " >>" + s[loop]);
}
}
}
This is the Student class:
public class Student {
private String name;
//private int age;
//public String gender = "na";
public static int instances = 0;
// Getters
//public int getAge() {
//return this.age;
//}
public String getName() {
return this.name;
}
// Setters
//public void setAge(int age) {
//this.age = age;
//}
public void setName(String name) {
if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
/**
* Default constructor. Populates name,age and gender
* with defaults
*/
public Student() {
instances++;
//this.age = 18;
this.name = "Not Set";
//this.gender = "Not Set";
}
/**
* Constructor with parameters
* #param age integer
* #param name String with the name
*/
public Student(String name) {
//this.age = age;
this.name = name;
}
/**
* Gender constructor
* #param gender
*/
//public Student(String gender) {
//this(); // Must be the first line!
//this.gender = gender;
//}
/**
* Destructor
* #throws Throwable
*/
protected void finalize() throws Throwable {
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
public String toString () {
return "Name: " + this.name; //+ " Age: " + this.age + " Gender: "
//+ this.gender;
}
public String getSubjects() {
return this.getSubjects();
}
}
and this is the MathStudent class which inherits from the Student class:
public class MathStudent extends Student {
private float algebraGrade;
private float calculusGrade;
/**
* Default constructor
* #param name
* #param algebraGrade
* #param calculusGrade
*/
public MathStudent(String name, float algebraGrade, float calculusGrade) {
super();
this.algebraGrade = algebraGrade;
this.calculusGrade = calculusGrade;
}
public MathStudent() {
super();
algebraGrade = 6;
calculusGrade = 4;
}
// Getters
public void setAlgebraGrade(float algebraGrade){
this.algebraGrade = algebraGrade;
}
public void setCalculusGrade(float calculusGrade){
this.calculusGrade = calculusGrade;
}
// Setters
public float getAlgebraGrade() {
return this.algebraGrade;
}
public float getCalculusGrade() {
return this.calculusGrade;
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" Math Student >> " + "Algebra Grade: " + algebraGrade
+ " Calculus Grade: " + calculusGrade);
}
}
Check how you instantiate students, i.e.
new MathStudent(Smith,14,15);
The name should be in quotes like "Smith"
new MathStudent("Smith",14,15);
Otherwise Smith will be interpreted as variable and this one is not defined.
Well, I need to implement two classes Student and School. Each student has an Id number consisting of number of arrival and the dormitory number, the second one should be random number from an array of elements.
Then I need to register these students to appropriate schools, there are four of them. This registration have to be random also. so I have Student and Schools classes here,
public class Student {
private String name;
private int id, size;
private int dorm;
/**
* Constructor of a class Student
*
* #param n sets the name of the student
*/
public Student(String n) {
name = n;
int d[] = {11, 19, 20, 22, 23, 24, 38, 39};
Random r = new Random();
dorm = d[r.nextInt(d.length)];
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the dorm
*/
public int getDorm() {
return dorm;
}
/**
* the id to set
*/
public void setId() {
id = size;
id = id*100 + dorm;
}
/**
* #return the id
*/
public int getId() {
return id;
}
#Override
public String toString() {
String result = "The student " + name + " has an id " + id + ". Located in the dormitory #" + dorm + ".";
return result;
}
And School class here,
import adt.Queue;
public class School {
private int size;
private String name;
private Queue q = new LinkedListQueue();
public School(String n) {
name = n;
size = 0;
}
public void register(Student st) {
q.enqueue(st);
size++;
}
public int getSize() {
return size;
}
public void viewStudents() {
System.out.println(q.toString());
}
#Override
public String toString() {
String str = "The School of " + name + ". The number of students is " + size + ".";
return str;
}
So as you can see, I have register method in the School class which register new Student to a specific school.
So my problem is that how can I get data size from a School class to use it in the Student class ???
I am only a student don't judge me please ^^)
When you add a student to the school, you should also tell the student what school they're in.
Add a field and a method to Student
public class Student {
private String name;
private int id, size;
private int dorm;
private School school;
public void setSchool( School school ) {
this.school = school;
}
Then add the code in School to tell the student.
public void register(Student st) {
q.enqueue(st);
size++;
st.setSchool( this );
}
Now as long as school is not null in Student, a student can find the size of their school.
If I understand your problem correctly, what you need is to add an int parameter to your student's setId() method:
public void setId(int id) {
this.id = id * 100 + dorm;
}
and then on the register() method you would do:
public void register(Student st) {
q.enqueue(st);
st.setId(size++);
}
I hope that helps you, good luck!
I'm fairly new to Java and having a hard time understanding how to access the methodss and setting variables to a specific value that are in my Bean class. I am creating new instances of the Bean class and wanting to set the variables to a specific value. I also want to store these all in a List. Here is the code I have:
public class ReportBean
{
//instance variables
private String inventoryFulfillmentSpecialist;
private String inventoryFulfillmentManager;
private String poNumber;
private String poReferenceNumber;
private String shipToLoc;
private String poStatus;
private String importStatus;
private String orderType;
private String item;
private String mismatchFields;
private String tValue;
private String hValue;
/**
* Getter method for variable 'inventoryFulfillmentSpecialist'.
* #return String
*/
public String getInventoryFulfillmentManager() {
return inventoryFulfillmentManager;
}
/**
* Getter method for variable 'inventoryFulfillmentSpecialist'.
* #return String
*/
public String getInventoryFulfillmentSpecialist() {
return inventoryFulfillmentSpecialist;
}
/**
* Getter method for variable 'poNumber'.
* #return String
*/
public String getPoNumber() {
return poNumber;
}
/**
* Getter method for variable 'poReferenceNumber'.
* #return String
*/
public String getPoReferenceNumber() {
return poReferenceNumber;
}
/**
* Getter method for variable 'shipToLoc'.
* #return String
*/
public String getShipToLoc() {
return shipToLoc;
}
/**
* Getter method for variable 'poStatus'.
* #return String
*/
public String getPoStatus() {
return poStatus;
}
/**
* Getter method for variable 'importStatus'.
* #return String
*/
public String getImportStatus() {
return importStatus;
}
/**
* Getter method for variable 'orderType'.
* #return String
*/
public String getOrderType() {
return orderType;
}
/**
* Getter method for variable 'item'.
* #return String
*/
public String getItem() {
return item;
}
/**
* Getter method for variable 'tssVsPoMisMatchFields'.
* #return String
*/
public String getTssVsPODirectMisMatchFields() {
return tssVsPODirectMisMatchFields;
}
/**
* Getter method for variable 'tradeStoneValue'.
* #return String
*/
public String getTradeStoneValue() {
return tradeStoneValue;
}
/**
* Getter method for variable 'hostValue'.
* #return String
*/
public String getHostValue() {
return hostValue;
}
/**
* Setter method for variable 'inventoryFullfilmentManager'.
* #param inventoryFulfillmentManager String
*/
public void setInventoryFulfillmentManager(String inventoryFulfillmentManager) {
this.inventoryFulfillmentManager = inventoryFulfillmentManager;
}
/**
* Setter method for variable 'inventoryFulfillmentSpecialist'.
* #param inventoryFulfillmentSpecialist String
*/
public void setInventoryFulfillmentSpecialist(
String inventoryFulfillmentSpecialist) {
this.inventoryFulfillmentSpecialist = inventoryFulfillmentSpecialist;
}
/**
* Setter method for variable 'poNumber'.
* #param poNumber String
*/
public void setPoNumber(String poNumber) {
this.poNumber = poNumber;
}
/**
* Setter method for variable 'poReferenceNumber'.
* #param poReferenceNumber String
*/
public void setPoReferenceNumber(String poReferenceNumber) {
this.poReferenceNumber = poReferenceNumber;
}
/**
* Setter method for variable 'shipToLoc'.
* #param shipToLoc String
*/
public void setShipToLoc(String shipToLoc) {
this.shipToLoc = shipToLoc;
}
/**
* Setter method for variable 'poStatus'.
* #param poStatus String
*/
public void setPoStatus(String poStatus) {
this.poStatus = poStatus;
}
/**
* Setter method for variable 'importStatus'.
* #param importStatus String
*/
public void setImportStatus(String importStatus) {
this.importStatus = importStatus;
}
/**
* Setter method for variable 'orderType'.
* #param orderType String
*/
public void setOrderType(String orderType) {
this.orderType = orderType;
}
/**
* Setter method for variable 'item'.
* #param item String
*/
public void setItem(String item) {
this.item = item;
}
/**
* Setter method for variable 'tssVsPODirectMisMatchFields'.
* #param tssVsPODirectMisMatchFields String
*/
public void setTssVsPODirectMisMatchFields(String tssVsPODirectMisMatchFields) {
this.tssVsPODirectMisMatchFields = tssVsPODirectMisMatchFields;
}
/**
* Setter method for variable 'tradeStoneValue'.
* #param tradeStoneValue String
*/
public void setTradeStoneValue(String tradeStoneValue) {
this.tradeStoneValue = tradeStoneValue;
}
/**
* Setter method for variable 'hostValue'.
* #param hostValue String
*/
public void setHostValue(String hostValue) {
this.hostValue = hostValue;
}
List<ReportBean> reportData = new ArrayList<ReportBean>();
ReportBean bean1 = new ReportBean();
ReportBean bean2 = new ReportBean();
ReportBean bean3 = new ReportBean();
bean1.setInventoryFulfillmentManager("Jackie Luffman");
bean1.setInventoryFulfillmentSpecialist("Phillip Smith");
bean1.setPoNumber("348794798");
bean1.setPoReferenceNumber("140629700");
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("ReportBean: ");
sb.append("inventoryFulfillmentManager = " + getInventoryFulfillmentManager() + " ");
sb.append("inventoryFulfillmentSpecialist = " + getInventoryFulfillmentSpecialist() + " ");
sb.append("poNumber = " + getPoNumber() + " ");
sb.append("poReferenceNumber = " + getPoReferenceNumber() + " ");
sb.append("shipToLoc = " + getShipToLoc() + " ");
sb.append("poStatus = " + getPoStatus() + " ");
sb.append("importStatus = " + getImportStatus() + " ");
sb.append("orderType = + " + getOrderType() + " ");
sb.append("item = " + getItem() + " ");
sb.append("tssVsPODirectMisMatchFields = " + getTssVsPODirectMisMatchFields() + " ");
sb.append("tradeStoneValue = " + getTradeStoneValue() + " ");
sb.append("hostValue = " + getHostValue() + " ");
return sb.toString();
}
}
I get errors and when I try to say bean1.setxxxx I get the red squiggly line and it's says "syntax error on token(s), misplaced construct(s)". If anyone could help it would be much appreciated, thanks.
bean1.setInventoryFulfillmentManager("Jackie Luffman");
bean1.setInventoryFulfillmentSpecialist("Phillip Smith");
bean1.setPoNumber("348794798");
bean1.setPoReferenceNumber("140629700");
This part of the code is just floating inside your class. This is a structured programming thing. In Java, all the code must be inside a method or function.
you must use a static main method to encapsulate this:
public static void main(String[] args) {
//yourcode here
}
import java.util.ArrayList;
class ReportBean {
private String InventoryFulfillmentManager;
private String InventoryFulfillmentSpecialist;
private String PoNumber;
private String PoReferenceNumber;
// Default constructor
public ReportBean() {}
public void setInventoryFulfillmentManager(String value) {
this.InventoryFulfillmentManager = value;
}
public void setInventoryFulfillmentSpecialist(String value) {
this.InventoryFulfillmentSpecialist = value;
}
public void setPoNumber(String value) {
this.PoNumber = value;
}
public void setPoReferenceNumber(String value) {
this.PoReferenceNumber = value;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<ReportBean> list = new ArrayList<>();
for(int i = 0; i < 3; i++) {
list.add(new ReportBean());
list.get(i).setInventoryFulfillmentManager("Jackie Huffman");
list.get(i).setInventoryFulfillmentSpecialist("Phillip Smith");
list.get(i).setPoNumber("348794798");
list.get(i).setPoReferenceNumber("140629700");
}
}
}
i have problem with printing the array after reading it. After printing, the address of memory is printed, not value of the array. What can i do for that ?
public class MyClass
{
Student St = new Student();
Student[]Array1 = new Student[10];
void AddList()
{
Scanner Scan = new Scanner(System.in);
for (int i=0; i<Array1.length & i<ArrayF1.length; i++)
{
System.out.println("Enter Student NAME Number " + (i+1) + ":");
Array1[i] = new Student();
Array1[i].setName(Scan.next());
//System.out.println("Enter Student MARK Number " + (i+1) + ":");
//St.setMark(Scan.nextFloat());
}
}
this is my print method. The result of print is like this
(studentproject.Student#1a758cb)
void PrintList()
{
for (int i=0; i<Array1.length; i++)
{
System.out.println(Array1[i]);
}
}
this is my Student Class that i have all my setter and getter method on that ... So i have 3 Class how can i work with this 3 class and in one of them get the data and in another print the Mark data and in third class print the Student Name data ... how can i do that ... i do some code but i dont know is it correct or not ... thanks for your help ...
public class Student
{
private String Name;
private float Mark;
/**
* #return the Name
*/
public String getName() {
return Name;
}
/**
* #param Name the Name to set
*/
public void setName(String Name) {
this.Name = Name;
}
/**
* #return the Mark
*/
public float getMark() {
return Mark;
}
/**
* #param Mark the Mark to set
*/
public void setMark(float Mark) {
this.Mark = Mark;
}
}
Just override the toString() method in Student class, and return the appropriate string you want to get printed when you print an instance.
It may look like: -
#Override
public String toString() {
return "Name: " + studentName;
}
Currently, the default implementation of toString() method of Object class is invoked, and what you are seeing is the format returned from that method, which is of the form - Type#hashCode
Here I've added some stuff how toString() method can be override
public class Student {
private String name;
private int id;
float mark;
public Student() {
}
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMark() {
return mark;
}
public void setMark(float mark) {
this.mark = mark;
}
#Override
public String toString() {
return "Student[ID:" + id + ",Name:" + name + ",Mark:"+mark+"]";
}
public void printStudentInfo() {
// print all the details of student
}
public static void main(String[] args) {
Student[] students = new Student[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < students.length; i++) {
System.out.println("Enter Student Name " + (i + 1) + ":");
String name = scanner.nextLine();
Student student = new Student(name, i + 1);
System.out.println("Enter Student MARK Number " + (i + 1) + ":");
float mark = scanner.nextFloat();
student.setMark(mark);
students[i]=student;
}
for(Student student:students) {
// by default toStirng method is called
System.out.println(student);
//or you can call like
//student.printStudentInfo();
}
}
}