You should capitalize names of your classes. After doing that do this in your school class,
Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);
Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. Ex: right click Source -> Generate getters and setters
Answer from ant on Stack OverflowYou should capitalize names of your classes. After doing that do this in your school class,
Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);
Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. Ex: right click Source -> Generate getters and setters
Try this :
public void addTeacherToClassRoom(classroom myClassRoom, String TeacherName)
{
myClassRoom.setTeacherName(TeacherName);
}
I am new to Java, and am currently studying how to create packages with related classes in Eclipse.
If I create one class (example: Circle) defining the methods ( writing()) I want to use, and another class using those methods, do I have to include a reference to an instance of that class? Here is a code example of what I mean:
public class Circle{
double r;
void wirte() {
Syste.out.println("Hello");
} and in the other class:
public class Circle2{
public static void main(String[] args) {
write();
}} Would that work? Or do I always have to follow that format: classinstance.methodname()?
Videos
UPDATE
I did not know I can do something like this
public class GUI(){
//do something
}
public static void main(String[] args){
new GUI();
}This code block worked for me --- thank you for pointing it out.
---------
So my IDE flagged an error at this line in main (from my main class Tic_Tac_Toe)
public static void main(String[] args) {
ColRowLists ttt = new ColRowLists();
char[][] board = new char[3][3];
System.out.println("Welcome to Tic Tac Toe");
userInput(board);
System.out.println("show me your list" + ttt.winOrLose()); //"'winOrLose(java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, ...)' in 'ThirtyMinutes.ColRowLists' cannot be applied to '()'"
}The winOrLose method in question lies in another class ColRowList: https://github.com/morry239/TTT_experiements/blob/master/src/ThirtyMinutes/Tic_Tac_Toe.java
I tried to do something like this, but it did not work obviously:
System.out.println("show me your list" + ttt.winOrLose(List topRow, List midRow, List botRow, List leftCol, List midCol, List rightCol, List diagonal1, List diagonal2);Could anyone kindly point me in the right direction? My main class is here FYI.
You need to somehow give class Alpha a reference to cBeta. There are three ways of doing this.
1) Give Alphas a Beta in the constructor. In class Alpha write:
public class Alpha {
private Beta beta;
public Alpha(Beta beta) {
this.beta = beta;
}
and call cAlpha = new Alpha(cBeta) from main()
2) give Alphas a mutator that gives them a beta. In class Alpha write:
public class Alpha {
private Beta beta;
public void setBeta (Beta newBeta) {
this.beta = beta;
}
and call cAlpha = new Alpha(); cAlpha.setBeta(beta); from main(), or
3) have a beta as an argument to doSomethingAlpha. in class Alpha write:
public void DoSomethingAlpha(Beta cBeta) {
cbeta.DoSomethingBeta()
}
Which strategy you use depends on a few things. If you want every single Alpha to have a Beta, use number 1. If you want only some Alphas to have a Beta, but you want them to hold onto their Betas indefinitely, use number 2. If you want Alphas to deal with Betas only while you're calling doSomethingAlpha, use number 3. Variable scope is complicated at first, but it gets easier when you get the hang of it. Let me know if you have any more questions!
Method 1:
If the method DoSomethingBeta was static you need only call:
Beta.DoSomethingBeta();
Method 2:
If Alpha extends from Beta you could call DoSomethingBeta() directly.
public class Alpha extends Beta{
public void DoSomethingAlpha() {
DoSomethingBeta(); //?
}
}
Method 3:
Alternatively you need to have access to an instance of Beta to call the methods from it.
public class Alpha {
public void DoSomethingAlpha() {
Beta cbeta = new Beta();
cbeta.DoSomethingBeta(); //?
}
}
Incidentally is this homework?
You need a reference to the class that contains the method you want to call. Let's say we have two classes, A and B. B has a method you want to call from A. Class A would look like this:
public class A
{
B b; // A reference to B
b = new B(); // Creating object of class B
b.doSomething(); // Calling a method contained in class B from class A
}
B, which contains the doSomething() method would look like this:
public class B
{
public void doSomething()
{
System.out.println("Look, I'm doing something in class B!");
}
}
In class WeatherRecord:
First import the class if they are in different package else this statement is not requires
Import <path>.ClassName
Then, just referene or call your object like:
Date d;
TempratureRange tr;
d = new Date();
tr = new TempratureRange;
//this can be done in Single Line also like :
// Date d = new Date();
But in your code you are not required to create an object to call function of Date and TempratureRange. As both of the Classes contain Static Function , you cannot call the thoes function by creating object.
Date.date(date,month,year); // this is enough to call those static function
Have clear concept on Object and Static functions. Click me