The Artima Developer Community
Sponsored Link

Java Answers Forum
anyone can help?

1 reply on 1 page. Most recent reply: Feb 23, 2003 8:24 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

anyone can help? Posted: Feb 23, 2003 5:21 AM
Reply to this message Reply
Advertisement
Can anyone help to see and explain the mistakes that i have made? Quite blur on concepts of constructors and references...thanx!

import java.io.*;
 
class Lab_03
{
    public static void main(String[] args) {
        System.out.print("\nPreset password (number only, 3 tries): ");
        int i = Integer.parseInt(getKeyboardBuffer());
        Password pass = new Password(i);
        
        while (pass.getTriesLeft() > 0) {
        	System.out.println("Enter password:");
       	}
    }
 
    private static String getKeyboardBuffer() {
         String s = "";
        try {
            s = new BufferedReader(
                    new InputStreamReader(System.in)).readLine();
        }
        catch (IOException e) {
		            System.out.println(e);
        }
        return s;
    }
}
 
class Password
{
    int grace = 3;
    
    int attempts = 0;
    	do {
    		attempts++;}
    		while (attempts <= grace);
    }
    
    private int magic;
        
    public Password(int password, int tries) {
        magic = password;
        attempts = tries;   
    }
 
    public int getTriesLeft(int attempts) {
    	int getTriesLeft = 3 - attempts;
  	    
    }
 
    public boolean verify(int password) {
       	if (magic == password)
       		System.out.println("Yes!");
       	else {
       		System.out.println("No!" + pass.getTriesLeft() + "tries left.");
       	}
    }
}
 


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: anyone can help? Posted: Feb 23, 2003 8:24 AM
Reply to this message Reply

import java.io.*;

class Lab_03{


public static void main(String[] args) {

System.out.print("\nEnter the Preset password: ");
int i = Integer.parseInt(getKeyboardBuffer());
Password pass = new Password(i);
while (pass.getTriesLeft() > 0) {
System.out.println("Enter password:");
pass.verify(Integer.parseInt(getKeyboardBuffer()));
}
}

private static String getKeyboardBuffer() {
String s = "";
try {
s = new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch (IOException e) {
System.out.println(e);
}
return s;
}
}

class Password{
private int magic = 314159; // digits for pi
private int grace = 3;
private int attempts = 0;


public Password(int password) {
magic = password;
}

public int getTriesLeft(){
return (grace - attempts);
}

public boolean verify(int password) {
attempts++;
boolean passwordVerified = false;
if (magic == password){
System.out.println("Yes! ");
passwordVerified = true;
}else{
System.out.println("No! " + getTriesLeft() + " tries left.");
}
return passwordVerified;
}
}

Flat View: This topic has 1 reply on 1 page
Topic: polymorphism and inheritance Previous Topic   Next Topic Topic: RMI Excute problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use