Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
class Lab2 {
int car1,car2,car3,m,n;
public void setNumber(int a) {
	n=a;
}
public void setSpeed(int a,int c ,int b) {
	car1=a;
	car2=c;
	car3=b;
}
public void setMax(int a) {
	m=a;
}
public int getNumber() {
	 return car1;
}
int getSpeed() {
	int [] myArray = new int[3];
        myArray[0] = car1;
        myArray[1] = car2;
        myArray[2] = car3;
        for(int i=1;i<myArray.length;i++)
      return myArray[i];
}
public int getMax() {
	 return car1;
}
public void display()
{
	
	System.out.println("Speed of cars\n \t"+getSpeed());
	System.out.println("N.o of cars:"+getNumber());
	System.out.println("Max speed:"+getMax());
}
}
class dehmi
{public static void main (String args[])
{
	Lab2 obj1= new Lab2();
    obj1.setNumber(3);
    obj1.setMax(90);
    obj1.getNumber();
    obj1.getMax();
    obj1.display();
}
}


What I have tried:

i'm using jcreator it's saying that the problem is in getspeed braket, help me plz i'm dying here ):
Posted
Updated 15-Apr-18 1:42am
Comments
Kornfeld Eliyahu Peter 15-Apr-18 7:10am    
Can you share the exact error - including line?
Patrice T 15-Apr-18 7:21am    
What does it say exactly.

I think you have forgotten something in your getSpeed function: why do you have a for loop whose body is just a return? And that will always just return the speed of car2

The chances are that is what the compiler is telling you about: there is a logical path through the method that does not meet a return statement - if the array.Length was less than or equal to one - OK, in practice it can't happen, but the compiler doesn't know that, so it rightly complains.

I suspect that you need to think a little more about what you are trying to do here, that code looks awfully "thrown together" without a lot of forward planning...
 
Share this answer
 
Java
int getSpeed() {
  int [] myArray = new int[3];

  myArray[0] = car1;
  myArray[1] = car2;
  myArray[2] = car3;
  
  for(int i = 1; i < myArray.length; i++)
    return myArray[i];
}

When you are formatting your getSpeed method it looks like this...
It looks very wrong, and probably not what do you meant... your for loop is actually empty (as it returns on the first iteration)... It also starts with a nice 1 where you array indexed from zero...
You have to redesign it according the specification you set for yourself for this method...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900