Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

02. Second Assignment The FacebookUser Class Were going to make a small change to the UserAccount class from the last assignment by adding a new

02. Second Assignment The FacebookUser Class

Were going to make a small change to the UserAccount class from the last assignment by adding a new method:

public abstract void getPasswordHelp();

This method is abstract because different types of user accounts may provide different types of help, such as providing a password hint that was entered by the user when the account was created or initiating a process to reset the password. Next we will create a subclass of UserAccount called FacebookUser. The FacebookUser class needs to have the following fields and methods in addition to what is in UserAccount:

Fields:

String passwordHint

ArrayList friends

Methods:

void setPasswordHint(String hint) void friend(FacebookUser newFriend) void defriend(FacebookUser formerFriend) ArrayList getFriends()

The friend method should add the FacebookUser argument to the friends ArrayList (if that FacebookUser is already in the friends list, display an error message), and the defriend method should remove the FacebookUser argument from that ArrayList (if that FacebookUser is not in the friends list, display an error message). The getFriends method should return a copy of this users friends create a new ArrayList with the same FacebookUsers in it as this users friends. Do not return the friends ArrayList directly, since this violates the principle of encapsulation. The getPasswordHelp method should display the passwordHint. The FacebookUser class should also implement the Comparable interface. FacebookUsers should be sorted alphabetically by username, ignoring capitalization. (Hint: notice that the toString method returns the username of the UserAccount.) Finally, write a driver program that creates several FacebookUsers and demonstrates the user of the setPasswordHint, friend, defriend, getFriends, and getPasswordHelp methods. Also, put the FacebookUser objects into an ArrayList, sort them (see the jar file for this topic for help on this), and print them out in sorted order.

You will be graded according to the following rubric (each item is worth one point):

  • The UserAccount class is abstract and has the requested fields and methods
  • The only abstract method is the getPasswordHelp method
  • The FacebookUser class extends UserAccount
  • FacebookUser has a friends ArrayList and methods to add, remove, and display the friends
  • FacebookUser implements Comparable and sorts alphabetically on username, ignoring capitalization
  • FacebookUser has a passwordHint that can be set through a method and is displayed by the getPasswordHelp method
  • The driver program creates a list of FacebookUsers
  • The driver program sorts and displays the lists of FacebookUsers
  • The driver program exercises the setPasswordHint, friend, defriend, getFriends, and getPasswordHelp methods
  • The program compiles and runs and the program is clearly written and follows standard coding conventions
  • Note: If your program does not compile, you will receive a score of 0 on the entire assignment
  • Note: If you program compiles but does not run, you will receive a score of 0 on the entire assignment
  • Note: If your Eclipse project is not exported and uploaded to the eLearn drop box correctly, you will receive a score of 0 on the entire assignment
  • Note: If you do not submit code that solves the problem for this particular assignment, you will not receive any points for the programs compiling, the programs running, or following standard coding conventions.

Here is my code

package calculator;

import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;

public class Calculator extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); }

/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

package calculator;

import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField;

public class FXMLDocumentController implements Initializable {

private Double curr_num, end_res; private String curr_operation; Boolean res_oper=false; @FXML private TextField textToDisplay;

@Override public void initialize(URL url, ResourceBundle rb) { }

@FXML private void handleDigitAction(ActionEvent event) { if(res_oper){ textToDisplay.clear(); res_oper=false; } String digit = ((Button) event.getSource()).getText(); String oldText = textToDisplay.getText(); String newText = oldText + digit; textToDisplay.setText(newText); }

@FXML private void handleOperationAction(ActionEvent event) { String text = textToDisplay.getText(); Double digit = Double.parseDouble(text); curr_num = digit; curr_operation = ((Button) event.getSource()).getText(); textToDisplay.setText(curr_num + curr_operation);

}

@FXML private void handleEqualOperation(ActionEvent event) { Double val1,val2,res; String val = textToDisplay.getText(); System.out.println(val); if (val.contains("-")) { String[] digits = val.split("-"); String digit1 = digits[0]; String digit2 = digits[1]; val1=Double.parseDouble(digit1); val2=Double.parseDouble(digit2); res=val1+val2; textToDisplay.setText(res.toString()); }else if(val.contains("/")){ String[] digits = val.split("/"); String digit1 = digits[0]; String digit2 = digits[1]; val1=Double.parseDouble(digit1); val2=Double.parseDouble(digit2); res=val1-val2; textToDisplay.setText(res.toString()); }else if(val.contains("X")){ String[] digits = val.split("X"); String digit1 = digits[0]; String digit2 = digits[1]; val1=Double.parseDouble(digit1); val2=Double.parseDouble(digit2); res=val1/val2; textToDisplay.setText(res.toString()); }else if(val.contains("+")){ String[] digits = val.split("\\+"); String digit1 = digits[0]; String digit2 = digits[1]; val1=Double.parseDouble(digit1); val2=Double.parseDouble(digit2); res=val1*val2; textToDisplay.setText(res.toString()); } res_oper=true;

}

@FXML private void handleClearAction(ActionEvent event) { textToDisplay.clear(); }

@FXML private void handleBackspaceAction(ActionEvent event) { StringBuffer sb = new StringBuffer(textToDisplay.getText()); sb = sb.deleteCharAt(textToDisplay.getText().length()-1); textToDisplay.setText(sb.toString());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

design a simple performance appraisal system

Answered: 1 week ago