问题
i need to write method for the website class, called browserLogin, which allows a browser who already has an ID to log in to the site. This method is passed a Browser object as a parameter and which uses the browser's setLoginStatus method to "log in" that browser to the website. It also needs to outputs a welcome message to a terminal window in the format Wine Direct welcomes browser 6732, you are now logged in.
private int yearOfBirth;
private int id;
private String email;
private boolean loggedIn = true;
public Browser(String getEmail, int getId, int getYearOfBirth)
{
email = getEmail;
id = getId;
yearOfBirth = getYearOfBirth;
}
public Browser()
{
email = "J.Booth@winedirect.com";
id = 2678;
yearOfBirth = 1990;
loggedIn = true;
}
public void yearOfBirth(int getYearOfBirth)
/**
*
*/
{
yearOfBirth = getYearOfBirth;
}
public void id(int getId)
/**
*
*/
{
id = getId;
}
public void setLoginStatus()
{
if(loggedIn = true)
{
System.out.println("online;" + id);
}
else
{
System.out.println("Offline");
}
}
public boolean isLoginStatus()
/**
*
*/
{
return loggedIn;
}
public void email(String getEmail)
/**
*
*/
{
email = getEmail;
loggedIn = true;
}
public void loggedOut()
/**
*
*/
{
email = "";
yearOfBirth = 0;
id = 0;
loggedIn = false;
}
}
public class Website
// instance variables - replace the example below with your own
private int hits;
private int salesTotal;
private Browser loggedIn;
private void browserLogin()
/**
*
*/
{
loggedIn
}
回答1:
Okay so let's start by writing your specifications into java code:
browserLogin method is passed a Browser object as a parameter
Your method doesn't have any parameters, so add it:
private void browserLogin(Browser br){
}
uses the browser's setLoginStatus method to "log in" that browser to the website
You specifically say to use the method, .. but the method is quite wrong.
if(loggedIn = true)
What this piece of code does is that it makes loggedIn true and then returns the value of loggedIn (which will always be true). Probably you meant:
if(loggedIn == true)
However it is not a setter method in any way. So assuming you want to log browser in, when it's not already logged in you could do something along these lines:
private void browserLogin(Browser br){
if(!br.isLoginStatus()){
br.setLoginStatus(true);
}
}
and edit your method to this:
public void setLoginStatus(boolean value)
{
loggedIn = value;
if(loggedIn == true)
{
System.out.println("online;" + id);
}
else
{
System.out.println("Offline");
}
}
From what I see, you just starting with Java. I would suggest reading the oracle tutorials and starting really from the beginning: Oracle Tutorials
来源:https://stackoverflow.com/questions/20130055/im-stuck-on-java-programing