I prepared an array of abstract classes called Person, but I cannot run it with the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0.
Person retains a person's name and money.
abstract class Person {
protected String names;
protected int money;
public Person (String name, int money) {
names=name;
money = money;
}
void getName(){
System.out.println("name:"+names);
}
void getMoney() {
System.out.println("money:"+money);
}
void changeMoney(int used_money){
money=moneyys-used_money;
if(money<0){
System.out.println("error:money cannot be less than 0");
}
}
void print() {
System.out.println(names+"now has"+moneyys+"yen");
}
abstract int getFee();
}
Below is the code that says if you can't pay when you get on the bus, you can't get on if the number of people is full.What I would like you to see is the passengers [pass_num++]=person; of void getOn() with ** on.This is where the error occurs.
public class Bus extensions Car {
private static int pass_num = 1;
private int pass_max;
private static int fee_sum = 0;
publicBus(intx){
pass_max = x;
}
Person passengers [ ] = new Person [pass_max];
int getBusNum(){
return super.num;
}
void getOn (Person person) {
int fee=person.getFee();
fee_sum+= fee;
if(person.moneyys-fee>=0&&pass_num<pass_max){
person.moneyys-= fee;
* passengers [pass_num++] = person; *
person.getName();
person.getMoney();
} else {
System.out.println(person.names+"could not get on the bus");
}
}
void getOff (Person pass) {
intk;
int flag = 0;
for(k=0;k<pass_num;k++){
if((pass.names).equals(passengers[k])){
System.out.println(pass.names+"got off the bus");
passengers[k].names=";
passengers[k].moneyys=-1;
flag = 1;
break;
}
}
if(flag==0){
System.out.println(pass.names+"was not on the bus");
}
flag = 0;
}
void printAllPassengers() {
for(int j=0;j>pass_max;j++){
if(passengers[j].moneyys!=-1){
System.out.println("name:"+passengers[j].names);
}
}
}
void printTotalFee() {
System.out.println("sum of fee:" + fee_sum);
}
}
Also, the Person class has a subclass that looks like this.
public class Adult extensions Person {
int getFee(){
return 200;
}
publicAdult(String name, int money){
super(name, money);
}
}
A similar class includes Child, Senior.
As a result of my research, I thought that the passenger is NULL or that it is not connected to the Person object, but I didn't understand.Professor, please.
Lastly, please forgive me for pointing out many things, such as the poor code and the lack of compatibility with what I want to program because I am just studying.
java
I can't see the whole thing well, so I'm not sure if this will solve the problem...
publicBus(intx){
pass_max = x;
}
Person passengers [ ] = new Person [pass_max];
Here
publicBus(intx){
pass_max = x;
passengers [ ] = new Person [ pass_max ];
}
Person passengers [ ];
Why don't you try it?
java.lang.ArrayIndexOutOfBoundsException
may be resolved.
public class Bus extensions Car {
private static int pass_num = 1;
private int pass_max;
private static int fee_sum = 0;
publicBus(intx){
pass_max = x;
}
Person passengers [ ] = new Person [pass_max];
You may be trying to set the value of pass_max
in the constructor first, and then try to secure a Person
array based on it, but that's not really the case.
It does not always run in the order it is written, and it has a set priority.
In this case, the constructor is called after the field is initialized first.
In other words,
Person passengers[] = new Person [pass_max];
When runs, the constructor is not yet running, and the pass_max
field of type int
remains at the initial value 0
.
The result is 0
elements in the array.
In order to run in the desired order, the constructor should also initialize using pass_max
.
public class Bus extensions Car {
private static int pass_num = 1;
private final int pass_max;
private static int fee_sum = 0;
publicBus(intx){
pass_max = x;
passengers = new Person [pass_max];
}
final Person passengers [ ];
Having a habit of giving final
constants is useful because you can detect these errors in compilation errors.
Note:
It has nothing to do with this issue, but if you comment on the code range that appeared in this answer,
pass_num
is a variable used as an array subscript, so the initial value should be 0
instead of 1
.static
© 2024 OneMinuteCode. All rights reserved.