views:

42

answers:

1

Hi! I just need some help with this program. The user has to enter in the id,password,the number of max tries & the number of max uses. And they have to go into a constructor... Could someone help me pass them into the constructor? I'm using java. Thanks!

import java.util.Scanner;
public class LoginPw{


 public static void main(String[] args){
  // TODO Auto-generated method stub
  Scanner reader = new Scanner(System.in);
  System.out.println("Enter in your id and password");
  int idnum = reader.nextInt();
  int password = reader.nextInt();
  System.out.println("Enter in the max number of tries");
  int maxtries = reader.nextInt();
  System.out.println("Enter in the number of max uses");
  int maxuses = reader.nextInt();
  PwLogin pwl = new PwLogin(idnum,password,maxtries,maxuses); 

 }}
class PwLogin{

  public PwLogin(int id, int pw){
   //assumes max num of tries is infinite. denote by value of 0.
   this(id,pw,0,0); //call the 4-argument constructor w/ maxTries & maxUses defaulted as zero(infinite)
  }
  public PwLogin(int id, int pw, int tries){
   //assumes max number of uses is infinite. denote by value of 0.
   this(id, pw, tries,0); //Call the 4-argument constructor with maxUses defaulted as zero (infinite)
  }
  public PwLogin(int id, int pw, int tries, int uses){
   // Now set the 4 class variables from the passed-in arguments
   this.idnum =id; 
   this.password = pw;
   this.maxtries=tries;
   this.maxuses=uses;

  }  

 }
+2  A: 

You need to declare member variables in Java:

class PwLogin{

    private int idnum; 
    private int password; 
    private int maxtries; 
    private int maxuses; 

    public PwLogin(int id, int pw){
        etc...
Mark Byers
So this would be the user inputted numbers?Like the ones i have reading in my main?do they have the same value as the userinputted ones in main?
Violet
Initially they are set to zero, but after you set them with the line 'this.idnum = id;' then they are set to the same value as you entered.
Mark Byers
Thank you thank you for your help! =]
Violet