Hello guys,
So if i have something like
numOfDay is from another class. How can I access it from this class?Code Java:int b= 0; if ( b < numOfDay){ exit; } else{ do this; } }
thanks
Printable View
Hello guys,
So if i have something like
numOfDay is from another class. How can I access it from this class?Code Java:int b= 0; if ( b < numOfDay){ exit; } else{ do this; } }
thanks
Hey IDK12,
You can do it like this:
Code Java:
Code Java:public class TestClass2 { int numOfDay = 420; }
Output:
b is less than 420
Thanks. Great example. I also have other questions. I amcreating a gpa calculator in a multithread( classes) and my friend told me not to do it that way until i learned all about the java concept. So I am bring them back to one single thread but i having problem. The average always gives me 0. Any ideas why ?
Code Java:import java.util.Scanner; public class Gpa { double finalGpa; double firstGpa; double gpaAverage; public void setGrade(double grade){ Gpa input = new Gpa(); double gpaGradeOne; double gpaGradeTwo; double gpaGradeThree; double gpaGradeFour; if(( grade <= 100 && grade > 89)){ input.calculatingGpa(4); } else if ( grade <=89 && grade > 79){ input.calculatingGpa(3); } else if(grade <= 79 && grade > 69){ input.calculatingGpa(2); } else if( grade <= 69 && grade >= 0) { input.calculatingGpa(1); } else{ System.out.println("Invalid"); } } public void calculatingGpa(double singleGpa){ firstGpa+= singleGpa; } public double get(){ return finalGpa = firstGpa; } public static void main(String[] args){ Gpa value = new Gpa(); Gpa calling = new Gpa(); Scanner get = new Scanner(System.in); int b = 0; double grade; System.out.println("How many class you have?"); int numClass = get.nextInt(); while ( b < numClass){ System.out.println("What is the gade?"); grade = get.nextInt(); value.setGrade(grade); b++; } System.out.println("GPAaverage is:" + calling.get()); } }
I think I might know. You need to call setGrade() somewhere.
Maybe like this:
Code java:private double grade; public GPA(double grade) { this.grade = grade; setGrade(grade); }
I think perhaps you're going about this the wrong way.
Here's what you should do.
- Have a GPA class. (You should need only 1 class).
- You can have a GPA in each class.
- You can use an array of type GPA(an array looks like this className[]. It has length a length, but since it starts at index 0, you want to use a for loop to stop before array.length. ) This array should go inside your main method.
- Make sure in your for loop to say to ask the user for each class to input the grade (circa 0-100) and then use that take initialize each GPA object in the array. Go from index 0 to index the number of classes the user said they had -1 in the for loop.
Code java:[*]gpa[index] = new GPA(valuePutInByUser);[*]
- You should have a GPA constructor with a double value.
- You should have a char variable called grade declared before the constructor and initialized inside of it.
- You should call setGrade(double value) inside that constructor. It should set the grade to the corresponding
- letter grade for each value. (e.g. if (value == 95) grade = 'A'. )
- You should have a char method called getGrade() .
- You should have a double method getGPA(char grade2). It should return 4.0 if grade == 'A', 3.0 if B and so on.
- You should have, inside your main class, a double variable called finalGPA, originally initialized to 0, that will later be set using a for loop. Inside the for loop, you should set finalGPA to finalGPA + the value of that index in the GPA array.
- You should create and initialize finalGPA before you reach the for loop.
- You should also have an int variable called numberOfClasses, created and initialized to 0 before reaching the for loop. When the user enters the number of classes, you change the value of numberOfClasses to that number entered.
Code java:[*]for (int i =0; i < array.length; i++)[*]{[*]finalGPA = finalGPA + array[i];[*]} [*]
- Then you have final GPA divided by total number of classes
Code java:[*]finalGPA = finalGPA/numberOfClasses;[*]
- Now you have your final GPA.
i think the problem lies with calling the object 'calling' instead of 'value' in your last prinln method:
the values of grade are stored in the value object and not the calling object.
We can not use neighbour's things directly. If we want to use then we should be relation or we have to ask neighbour. But you can not use if the thing is privacy for them .
Likewise in java world, in order to access another class members we have to be relation(means child class) or we have to use object.
There're many things to say about this, but her i am saying only few things. Let's see one simple example:
class first
{
public int a;
private int b;
protected int c;
//rest code
}
class second extens first //child of first
{
//rest code
public void access()
{
S.o.p(a);
}
class third
{
//rest code
void method1{
first f=new first();
f.a=2;
}
}
in the above code b can not accessed outside of the first class. C can be used by first class and their child class.