I am still fairly new to coding in java and I am trying to build a text game to help me learn. I am having issues transferring a variable to help me test out specs.
String a = user.next();
if(a.equals("warrior")){
System.out.println("You start to feel stronger, feels like you could run through a brick wall and keep on going without a scrath on you.\n" +
"Of course you might want to wait on that for awhile.\n" +
"********************************************************\n*\t\tAchievement got: Dumb as a rock!\t\t\t\t\t\t *\n********************************************************");
String specW = ("warrior");
comW (warrior);
}
public static void comW (warrior){
System.out.println("Testing1");
}
is anyone able to help me figure out why I keep having errors?
comW (specW);
}
public static void comW (String warrior){
System.out.println(warrior);
}
Do above changes in your code.
Basically you are passing a variable in comW method but the variable is not declared.
Secondly, in your comW method you didn't give parameter type.
I would recommend you to follow some tutorials carefully before posting question here.
You are creating a string variable with name specW but passing a variable warrior to comW function. So change the function parameter to specW. Also in the definition of function comW the parameter warrior is not given any type so give it a String type.
String a = user.next();
if(a.equals("warrior")){
System.out.println("You start to feel stronger, feels like you could run through a brick wall and keep on going without a scrath on you.\n" +
"Of course you might want to wait on that for awhile.\n" +
"********************************************************\n*\t\tAchievement got: Dumb as a rock!\t\t\t\t\t\t *\n********************************************************");
String specW = ("warrior");
comW (specW); // 1st Change
}
public static void comW (String warrior){ // 2nd Change
System.out.println("Testing1");
}
Related
I have automated a new customer form for work, but there are a lot of options and based on how questions are answered , different fields need to be filled out. Rather than just make a copy of the code and make a different script for each option, I'd like to do this by passing values to a class that determines what options are chosen based on what is passed in. I'm trying to figure most of this out myself and I'm somewhat of a n00b, but if someone can get me past the first hurdle, I'd like to tackle the rest of the hurdles myself.
So I want to start by just doing one line of the script this way, and eventually I will do more. Up front, it is going to seem like a lot of code just to do this, but here is the line:
driver.findElement(By.id("OrganizationName")).sendKeys("The Rolling Stones");
Here is what I have so far:
ncformPage1 skifootz = new ncformPage1("Rolling Stones");
skifootz.getOrgname();
That is the part that is in the script. Here is the class I wrote:
public class ncformPage1 {
private String orgName;
public ncformPage1(String on) {
orgName = on;
}
public String getOrgname() { return "driver.findElement(By.id(\"OrganizationName\")).sendKeys(\""
+ orgName + "\");";
}
}
So when I run this, it goes right past that organizationName element and leaves it blank, does all the other elements, and then fails because organization name is a required field. So I added this bit of code here to see what it prints out to the console:
System.out.println( skifootz.getOrgname());
Sure enough, it prints out
driver.findElement(By.id("OrganizationName")).sendKeys("Rolling Stones");
Which is exactly what I want returned. (I think the last semicolon is extraneous in this case, but at least it returned what I wanted!) But it doesn't execute that. I've tried all kinds of stuff to get it to execute, such as removing driver from what is returned and appending it here instead:
driver.skifootz.getOrgname();
but that gives me skifootz cannot be resolved or is not a field. I tried this:
String a = skifootz.getOrgname();
driver.a();
But that just made a get underlined in red saying method a() is undefined for the type Webdriver. So then I changed String a to Webdriver a:
WebDriver a = skifootz.getOrgname();
driver.a();
But now skifootz.getOrgname(); is underlined saying "type mismatch: cannot convert from String to WebDriver." I've been messing around with it for a few days now, and I haven't gotten any closer. Maybe this is an easy solution, but if I can just get this part working then perhaps I can move on to the next phase? This n00b thanks everyone in advance for any help anyone can give.
The method is returning a String type and you expect it to act like a driver object. That part is incorrect.
I think you can write methods to be more like
public WebElement getOrgname(WebDriver driver, String OrganizationName) {
return driver.findElement(By.id(OrganizationName));
}
WebElement a = skifootz.getOrgname(driver);
a.sendKeys("Rolling Stones");
OR
public void TypeText(WebDriver driver, String OrganizationName, String TextToType) {
driver.findElement(By.id(OrganizationName)).sendKeys(TextToType);;
}
in your context this should probably work.
ncformPage1 skifootz = new ncformPage1();
skifootz.getOrgname(skifootz.driver, "OrganizationName");
skifootz.sendKeys("Rolling Stones");
public class ncformPage1 {
private String orgName;
public WebDriver driver = new ChromeDriver(); // I'm assuming this part.
public ncformPage1(String on) {
orgName = on;
}
public WebElement getOrgname(WebDriver driver, String OrganizationName) {
return driver.findElement(By.id(OrganizationName));
}
}
this is my first question on here and I did a search before forming it, so I hope everything is as required.
I am working on a school assignment in Java. I am able to produce the required output but there are a lot of null instances created first. I don't understand why. Information about the library the professor created for the course and the code are below
Library included with this course: i2c.jar. It can be found here.
included in this Library are the classes Country and CountryDB. The API for the Country class can be found at http://130.63.94.24/~roumani/book/doc/i2c/ca/roumani/i2c/Country.html
The API for the CountryDB class can be found at http://130.63.94.24/~roumani/book/doc/i2c/ca/roumani/i2c/CountryDB.html
I am asked to create a class called Game, using the Country and CountryDB APIs.
The only attribute is db, which is an instance of CountryDB.
The constructor only sets the attribute (db) for this instance to a new CountryDB object.
The class is also meant to include a method (called qa) that follows this pseudocode:
get a reference to the database's capital city list
determine the size of this list. Cal it n.
generate a random number in [0,n) called index.
invoke get(index) on the list to get a random capital city. Call it c
get a reference to the database's data map
invoke get(c) on the map to get a reference to a country. Call it ref.
The method is then supposed to return one of two Strings (which will be clear in the code). Everything works as it should, except I get a lot of "nulls" before the desired output. When made into a List, db has size 241 so I suspect I am creating 241 null instances and 1 proper instance. I have no idea why though. I have tested every line of code in my method and the constructor was dictated by the textbook.
CODE
package ca.yorku.eecs.caps;
import java.util.List;
import java.util.Map;
import ca.roumani.i2c.Country;
import ca.roumani.i2c.CountryDB;
public class Game
{
private CountryDB db;
public Game()
{
this.db = new CountryDB();
}
public String qa()
{
List<String> capitals = db.getCapitals();
System.out.println(capitals.toString());
int n = capitals.size();
System.out.println(n);
int index = ((int) (n * Math.random()));
System.out.println(index);
String c = capitals.get(index);
System.out.println(c);
Map<String, Country> data = db.getData();
Country ref = data.get(c);
if (Math.random() > 0.5)
{
return "What is the capital of " + ref.getName() + "? \n" + ref.getCapital();
}
else
{
return ref.getCapital() + " is the capital of? \n" + ref.getName();
}
}
public static void main(String[] args)
{
Game g = new Game();
System.out.println(g.qa());
}
}
the System.out.println() statements are only there to test when the nulls occur. It clearly happens immediately because my psvm output is 241 nulls (on separate lines) followed by my desired output. Can somebody please tell me what I am doing wrong?
And, more generally (to help more people) how do you implement classes, the constructor of which instantiates another class and sets it as an attribute value?
I appreciate any help. Also, please note, I am not trying to get others to do my work for me. I've spent hours on this and my lab TA also wasn't sure why it happens either. He would have helped me correct it had he known how.
Thank you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I need help with my homework. Please note that I have finished the code below myself, but am not sure if I am doing it correctly, especially the last sentence of my homework assignment.
My homework assignment:
Define a class called Building with the following properties. Every Building has a square footage (area) and stories. The constructor creates a Building with these two attributes. The methods get_squarefootage(), get_stories(), set_square_footage(), and set_stories() will be used to get and set the respective attribute values. The method get_info() will return all the current attribute values of the Building. Write a program that lets a user create Building objects and change their attribute values.
package building_hw2;
import java.util.Scanner;
public class Building {
int area;
int stories;
int get_squarefootage() { //get values of the area
return area;
}
int get_stories() { //get values of the stories
return stories;
}
void set_square_footage(int area) { //set values of the area
this.area = area;
}
void set_stories(int stories) { //set values of the stories
this.stories = stories;
}
void get_info() { //return all the current attribute balues of the building
System.out.println("The square footage of the building is " + area);
System.out.println("The building has " + stories + " stories");
}
//main method
public static void main(String[] args) {
Building Bldg = new Building(); //create a building object
Bldg.area = 40000;
Bldg.stories = 5;
Bldg.get_info(); //display the current values of the building
//get user input to create building object
Scanner keybd = new Scanner(System.in);
System.out.println("Please enter the square footage(area) of the building : ");
int bldgArea = keybd.nextInt();
System.out.println("Please enter the stories : ");
int bldgStories = keybd.nextInt();
Bldg.set_square_footage(bldgArea);
Bldg.set_stories(bldgStories);
Bldg.get_squarefootage();
Bldg.get_stories();
Bldg.get_info();
}
}
You seem to be doing it correctly. However I would like to point out a few things. First you should declare the member variables as private for better encapsulation of your class. You already have setter methods for changing the attribute value.
int area;
int stories;
In your main you can set building values as follows :
Bldg.set_square_footage_area(40000);
Bldg.set_stories(5);
The requirement for get_info is not very clear you should ask what exactly should be returned (some string representation of the attributes or just print current values of all attributes)
You're confusing "returning" and "printing to the console". The get_info() method should return something. It shouldn't print anything.
Fields should be private. Methods, in this case, should be public, since you want any other class to be able to call them. And you forgot to provide a constructor, althoughh your teacher asked you to provide one.
Please kindly inform your teacher that naming conventions exist in Java, and that teaching other conventions is not a good idea at all. A whole lot of frameworks and APIs assume the respect of the standard conventions. get_squarefootage() should be named getSquareFootage(). Same for the setter. Use real words, starting with a lowercase letter, for your variables: building, and not Bldg.
get_info should either return a String or be renamed to printBuildingAttributes.
You have Bldg.area, but why not have a setter method for this field? That would jive with your getter/setter paradigm. It looks like you already have it, so use it. Make the fields themselves private and only accessible thru your getter/setter methods.
If you are going to retrieve the number of stories or area of a building, you need to store it in a variable. Right now, you are retrieving it and throwing it away.
Also, get_info or get_area is not the right Java convention for method naming.
I want to create an object of a class by referring to its object, I think. I've been able to make it in C# but in Java it wont work. This is what I want to do:
controller.getDal().getStudentData().getPerson() = new Person(student.getIdNumber(), student.getName(), student.getAddress(), student.getTel());
But I get a error message saying:The left-hand side of an assignment must be a variable
How can I fix the problem? I've tried like doing like this:
register.AddStudent(controller.getDal().getStudentData().getPerson());
and then
System.out.println("Show info: " + controller.getDal().getStudentData().getPerson());
and the output I get is : Person#7cd0a5d9
Java doesn't have the Property syntax that c# does. you have to use a setter.
controller.getDal().getStudentData().setPerson(
new Person(/*blah blah blah*/)
);
if you control whatever type getStudentData returns, than you might have to make one.
public void setPerson(Person newPerson)
{
this.person = newPerson;
}
Right now you are trying to set a new person using a get method. You cannot set an object to a function. You are on the right track with your code:
register.AddStudent(controller.getDal().getStudentData().getPerson());
I do wonder however if in your code that a student and a person are the same thing. You did not provide enough code for me to test and to give you an guaranteed answer, but I would assumg that your code should be more like this:
register.AddStudent(controller.getDal().getStudent());
This way you are getting the student and then adding the student. I'm not sure what you are trying to accomplish but you should really be looking into set methods such as something like:
Person p = controller.getDal().getStudentData().getPerson();
p.setIdNumber = 0011559966
p.setAddress('123 C St');
Or even something along the lines of:
register.AddStudent(new Student("Billy", "Crystal", "123 C st"));
Anyway, if you had more code, I would be able to help you more, but that is the best I can think of without any real context
About:
System.out.println("Show info: " + controller.getDal().getStudentData().getPerson());
You must override the toString() method inside the Person class to the fields or string representation you want to see upon printing.
An example could be:
#Override
public String toString() {
return "Name: " + this.getName() + " Id Num: " + this.getIdNumber();
}
I am somewhat new to JAVA. I've been working with it in college, but I have to admit, my instructor is of absolutely no help. She hardly knows JAVA herself, but that is another issue all in itself. I've been confused as to how methods and classes work. I'm creating this program that uses two files, one "main" file, and a "test" file. I can't seem to get the "main" file correct, as the compiler keeps telling me that it cannot find the symbols, even though they are. In the "test" file, I can't seem to get the compiler to recognize the methods from the "main" file. I have made sure that the files are in the same folder. I want to combine them into one file for simplicity, but I will lose points. I've included my code so far. I'm not looking for a "fix-it" solution, I just want to figure out why it's not working. ANY help is appreciated, since my instructor isn't of much assistance Thank you kindly!
MAIN FILE:
import java.util.Scanner;
class Fruit1 {
static Scanner console = new Scanner(System.in);
public static void main(String args[]) {
String color;
String taste;
}
public Fruit1() {
// generic constructor
color = "red";
taste = "yum";
}
public Fruit1(String aColor, String aTaste) {
// constructor with parameters
color = aColor;
taste = aTaste;
}
public Fruit1(String bColor, String bTaste) {
color = bColor;
taste = bTaste;
}
String getTaste() {
return taste;
}
String getColor() {
// Accessor method
return color;
}
}
TEST FILE:
import java.util.*;
public class Fruit1Test {
static Scanner console = new Scanner(System.in);
public static void main(String args[]){
Fruit1 a = new Fruit1("pinkish-red", "sweet-tart");
Fruit1 l = new Fruit1("yellow", "tart/sour");
a.taste();
a.color();
l.taste();
l.color();
System.out.println("Your apple is " + a.color + "in color and has a " + a.taste + " taste. ");
System.out.println("Your lemon is " + l.color + "in color and has a " + l.taste + " taste. ");
System.out.println();
}
}
a.taste(); will try to find method taste(); in your main file i.e. in Fruit1.java file. However as same is not found, it will throw error at compile time only that Method taste() is not found...
All below 4 statements will FAIL as those are not present...
a.taste();
a.color();
l.taste();
l.color();
As you are creating object of class by using below statement, already values to taste and color by use of constructor public Fruit1(String aColor, String aTaste){.
Fruit1 a = new Fruit1("pinkish-red", "sweet-tart");
I believe you now want to print the values of color and taste. To print those use getter methods that you have (getColor() & getTaste())
System.out.println("Your apple is " + a.getColor() + " in color and has a " + a.getTaste() + " taste. ");
System.out.println("Your Lemon is " + l.getColor() + " in color and has a " + l.getTaste() + " taste. ");
Note
You don't need to write public Fruit1(String bColor, String bTaste){ again as you have already defined above that....
Also your below statement should be before constructor and out of psvm
String color;
String taste;
Let me know if you are unclear...
Good Luck
You never declare the fields color or taste for the object Fruit1. Instead, you created the variables in the main method.
I suggest you read some basic tutorials on Java to get the hang of things. (Oracle also provides more advanced tutorials.)
I noticed in Fruit1, you are declaring the member variables in function main(). From the looks of it, Fruit1Test should have a main() fxn but Fruit1 should not. Take out those member variables out of main() and get rid of main() in Fruit1 (put it under the 'console' variable).
I also noticed that you have 2 constructors that both take in Strings. The compiler will probably complain about that too. I don't have a compiler in front of me but that's what I can tell just from looking.
For your first problem, it seems you're misunderstanding how to declare instance fields. You're creating them inside the main function, when you should create them directly inside the class.
For your second problem, see Fahim Parkar's comment, if it applies to your case. BTW it's a good practice to always have only one class/interface/enum per file, and have the file with the same name of the class (this second part may be mandatory in Java, I don't remember for sure - it applies to public classes, but I dunno if it also applies for "default, package protected" ones).
If they're named correctly, OTOH, maybe the error is because your "main" file didn't compile, so the "test" one didn't find it...
P.S. I just noticed you have two constructors with the same signature (number of parameters and same parameter types). You must remove one.