Using Command Pattern For Undo And Redo In ArrayLists

Apr 15, 2015

So I have a program where you can log in and add/remove friends to and from the friends arraylist. Also I can like a certain thing and that thing will be stored into the likes arraylist. I'm asked to make undo and redo options for whichever action I do.I want to add apple as a friend. After that when I select the undo option, I can undo that action so apple wouldn't be my friend. How I can approach this with a Command Pattern when the input is whatever name or word I inputted to store into the friends arraylist?

I did some research and found that using a command pattern could be my best bet since this has to be done under the Facebook Class I already have. I decided to add parts of what I have ...

In the driver program

Facebook facebook1 = new Facebook();

if (userInput == 6) {
System.out.println("Login");
String operand1 = getOperand("What is the Username? ");
String operand2 = getOperand("What is the Password? ");
System.out.println("Enter a friend to be added. ");
String operand3 = getOperand("What is the Username? ");
facebook1.friend(operand3);

[code]....

View Replies


ADVERTISEMENT

How To Implement UNDO Feature

Mar 10, 2014

I need to design 'notepad' application as my assignment.

Requirement: Only text is allowed, all the characters are having same size and font. It should have feature like new, open, save , save as , exit , UNDO , find , replace and font.

For this i use JTextArea and menu bar .

I am able to develop features like new, open, save , save as , exit. Need to implement now UNDO , find , replace and font but struck at 'UNDO'.

UNDO feature should be enable only when user writing in the writing area but not when it opens any file. What i thought is to have a flag on whenever user save whatever he wrote and if he use select UNDO then it check for the flag. If flag is ON then it will not do UNDO and if flag is not set then it cleared everything from the Text Area.

View Replies View Related

Undo Option In Jtable?

Apr 24, 2014

I am trying to create a JTable and I want to implement an undo option in it. Is it possible that I store the table model at some part of the operation and later when i press an undo button or something, I can set the saved table model so that it will perform like an undo.

View Replies View Related

Why Command Prompt Does Not Recognize Java As A Command

Jul 22, 2014

I am trying to execute a program from the command prompt. I type java -jar zuul.jar (zuul is the name of my project) and I get a message that java is not recognized as an internal or external command. What do I do wrong?

View Replies View Related

How To Send Command To Command Line

Jun 19, 2014

I simply cannot understand and find how to send a command to Command Line from Java.OK I can Open DOS:

1. Process p=Runtime.getRuntime().exec("cmd /c start");

2. Now How to "cd C:" + Enter ?

3. send another command "mvn clean install" + Enter

View Replies View Related

ArrayLists Objects Checking

Jun 27, 2014

I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .

The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.

View Replies View Related

Using Iterators To Compare ArrayLists

Sep 15, 2014

The question states:

>Suppose that aList and bList are instances of java.util.ArrayList. Use two iterators to find and display all the objects that are common to both lists. Do not alter the contents of either list. Write the segment of code assuming that the objects are of type String.

If my understanding is correct, I need to create two Iterators (one for aList and one for bList) to compare both ArrayLists and I should output any value that appears in both lists.

This is my attempt at it:

import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> aList = new ArrayList<String>();
ArrayList<String> bList = new ArrayList<String>();

aList.add("One");
aList.add("Two");

[Code] ....

The output:

`One`

`Two`

It stops there. It's clear that my loop is wrong, but I don't really know how to go about fixing it.

View Replies View Related

Use If / Else Statement That Depends On Which Of 2 ArrayLists Contain Object

Sep 9, 2014

I have two arraylists. One is personalContactList. The other is businessContactList.

1. I want to take user input that references an object attribute (int),
2. Use it to determine which object is referenced,
3. Find the "type" attribute of that object,
4. Determine which arraylist the object belongs to, based on that type,
5. Use the if else/statement to print out some attributes that depends on which arraylist the object is in.

I believe the code successfully does 1-3, and probably 4. But there is a hangup on 5. I get an indexoutofbounds execption. Which I'm not sure I understand very well.

Here is the code--it's part of a switch statement:

case "c": {
//some code here prints out a list
boolean b = false;
int g; 
while (b == false) {
  Scanner detailScanner = new Scanner(System.in);
System.out.print("Enter a number to see details: ");
 
[Code] ....

View Replies View Related

User Inputting Objects Into ArrayLists

Feb 25, 2014

I'm new to java and up until now whenever I have created an object it has been via the BlueJ interface. I would make an object manually and then manipulate it with my methods.

I have made a basic program which adds objects into arraylists via the BlueJ interface. However I now need to prompt a user to enter a string which should create the specific object and add to its ArrayList.

Is there a way to create objects without manual specifying the parameters? I can provide some of my code if it is needed I am trying to make a crude version of Plant vs Zombies for an assignment.

I am aware of the scanner class but I only know how to prompt a user but I feel like if I can figure a way to create the objects I can dynamically I can associate the string inputs to the proper arraylists.

View Replies View Related

Between Two ArrayLists / How To Find Which Elements They Have In Common

Apr 6, 2015

I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering
in what other ways, perhaps APIs i can use to do that?

View Replies View Related

Code Which Adds Marks Into 2 Different ArrayLists

Jan 27, 2014

I have this piece of code, which adds marks into 2 different arrayLists, one for homework marks, and one for examination marks..

ArrayList<Double> homeworkMark = new ArrayList<Double>();
ArrayList<Double> examinationMark = new ArrayList<Double>();
boolean markCheck = true;
do{
//
the purpose of this try catch is to make sure that the entered mark is a valid number. If the program encounters an exception markCheck will become true and the loop will begin again asking the user to enter the marks. If the marks are all encountered correctly markCheck will be false and the loop will end.

try{
for (int i = 1; i <= amountAssignment; i++ ) {
sc.reset();
System.out.print("Homework Mark for Assignment " +(i) + ": ");
homeworkMark.add(sc.nextDouble());
System.out.print("Examination Mark for Assignment " + (i) + ": ");
examinationMark.add(sc.nextDouble());
markCheck = false;
 
[code]....

View Replies View Related

HashMaps / ArrayLists - Diamond Operator?

Jul 8, 2014

So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?

Java Code:

import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();

map.put("California","Sacramento");
map.put("Oregon","Salem");
map.put("Washington","Olympia");

System.out.println(map);
}
} mh_sh_highlight_all('java');

View Replies View Related

Linking Elements Of Two Arraylists Together For A Bubble Sort?

Jun 13, 2014

I have been doing a program that allows the user to input students names and numerical grade values. I have already completed 3/4 of the program but am stuck on the bubblesort method as this is what it requires:

The program must have an indefinite loop, which prompts the user to select a sorting criterion or to end the program, and must also use bubble sort. Note: The user may either select name or grade as the sorting criteria. The program must use bubble sort to sort that data according to the specific criteria and then use another loop to display the data. This process must continue until the user ends the program.

My problem is, while i can easily do the bubblesort, where to look on how to link elements of the two arraylists I'm using (one for grades, one for names) so that when the user decides what sort they want, the individual's name and grade stays together.

View Replies View Related

Drawing A Graph Based On Stock Values From 2 ArrayLists?

Apr 19, 2015

[URL] I made a program that takes 2 stock values by URL tickers, and now I need it to draw a graph with the values for the 2 stocks, so they can be compared visually. So far my idea was to create 2 arraylists that consist of the values for each stock, so that I can draw the graph so that x always move with 1 step, and y0 is the first coordinate, and y1 is the second, and after that y1 is the first, y2 second etc.

View Replies View Related

Text Based RPG Game - Working With Multiple ArrayLists

Feb 22, 2014

I have come across an issue with arraylists. I am writing a text based RPG game as something to start with ...

Initially I had a single zone which was all stored in an arraylist and everything was working in regards to the player moving around. The problem I now have is how to add further zones to my game. Ideally I would like an arraylist for each zone, and would use the below to create each arraylist

public static ArrayList<RoomsClass> castleMap = new ArrayList<>();

The problem I now have is how to handle the player moving, initially with a single zone/arraylist I could reference that arraylist directly

public void findRoomCoords(int ID) {
for (int i = 0; i < castleMap.size(); i++) {
if (castleMap.get(i).roomID.equals(ID)) {
PLAYER.setCurrentRoomZone(castleMap.get(i).roomZone);
PLAYER.setCurrentRoomX(castleMap.get(i).roomX);
PLAYER.setCurrentRoomY(castleMap.get(i).roomY);
PLAYER.setCurrentRoomZ(castleMap.get(i).roomZ);
}
}
}

My initial thought was to use a getter/setter to remove the reference of castleMap from my movement code in order to access different arraylists, however this is where things have fallen over, I can't seem to work out how to get the arraylist name to change, depending on the outcome of the setZoneMap() method.

public void setZoneMap() {
switch (PLAYER.getCurrentRoomZone()) {
case 0: {
zoneMap = Castle.castleMap;
break;

[Code] ....

View Replies View Related

Populate DB From Another Using ArrayLists And Arrays - Index Out Of Bounds Exception

Mar 16, 2015

I have an issue with an IndexOutOfBoundsException. I am trying to populate a db from another using arraylists and arrays. I can get the data but the program fails when trying to run the inserts. I am trying to perform SQL in batches of 5. I have added a comment to the failing line.

/*Set in code at beginning*/
ArrayList<String[]> privacyList = new ArrayList<String[]>();
ArrayList<String[]> statementBuffer = new ArrayList<String[]>();
 
/*Some sql is performed and the following String array is populated*/
while (rs.next()) {
String[] row = new String[55];
resultSetIsEmpty = false;
row[0] = rs.getString("ID");

[Code] ....

View Replies View Related

Save Class With Arrays So As To Reload Them Again And Hold Onto List Of Objects Within Those ArrayLists

Dec 7, 2014

I have a class with static ArrayLists to hold objects such as Members,Players etc.I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.

The ArrayClass

import java.io.Serializable;
import java.util.ArrayList;
public class ArrayClass implements Serializable {

[code]....

The arrays within the ArrayClass are empty when i reload the application.I cant tell if the arrays are being properly saved or is it in the reloading from file???

View Replies View Related

How To Add Spaces To A Pattern

Apr 21, 2015

I have created this program that outputs

1
12
123
1234
12345
123456

I tried looking up some ways to create spaces in between each number, but I failed miserably. Here is what I have right now.

public class Iterations {
public static void main(String[] args){
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j);

[Code] ....

View Replies View Related

Printing Pattern When Name Is Given

Mar 10, 2014

I want to know how to print this type of pattern when a name is given ???

Example:

name given TEJA

Capture.PNG

View Replies View Related

How To Apply Format Pattern

Jan 12, 2015

I am able to get Cpu speed using my GetProcessorSpeed method and It returns this output 1796. How can apply this pattern "#.##". I am trying something like this.

Format formatter=new DecimalFormat("#.##");
formatter.format(MainClass.GetProcessorSpeed());
label2.setText(formatter.toString());

View Replies View Related

Head First Design Pattern

Jan 11, 2014

I was reading head first java and the author told to read head first design pattern next.

View Replies View Related

String Pattern Match

Apr 24, 2015

Why the following string fails the test below:

@Pattern(regexp = "^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.(([0-9]{1,3})|" +
"([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$", message = "Not a well-formed email address")

View Replies View Related

Pattern Matches In Java

Jul 3, 2014

I have code which validate code enter by user

the requirement say the maxlength=2 and minlength=1 and is a string

the user can enter code as follows

00
A1
HH
12
10
09

I have this Java Code:

public boolean isValidPattern(String s_value, String s_pattern) {
boolean flag = false;
if (Pattern.matches(s_pattern, s_value)) {
flag = true;
}
return flag;

[Code] ....

My problem is when user put

A1 AM geting error

View Replies View Related

Create A Pattern Of Stars

Jul 2, 2014

So I'm studying for a test i have coming up on recursion so i was playing around with it. I ended up making a pattern of stars that look that such :

*
**
***

The code i had for that was

public void makePattern(int size){
if(size == 0){
System.out.println();
}
else if(size > 0){
System.out.print("*");
makePattern(size - 1);
}
}

I was wondering if i wanted to expand on this and make it do something like

*
**
***
**
*

What condition would i have to add i was struggling trying to figure it out...

View Replies View Related

Inheritance For Word Pattern

Apr 24, 2014

I am in an intro programming class and we got assigned a problem for creating a super class with about a dozen sub classes for generating a random word(via WordGetter class) and then comparing that word to a variety of different patterns(like: does the word contain "re"). We were given the super class which looks like this...

public class Pattern {
public boolean matches(String text) {
return true;
}
public String toString() {
return "(TRUE)";

[code]...

and from this class, we have to write subclasses that override those three methods. I am struggling to understand inheritance and I am not really sure where to even start. Here is the instructions for the first sub class we need to write...

"CONTAINS" SUBCLASS
Constructor: The constructor accepts a String named ‘letters’.

Matches: This pattern matches any text that contains at least one occurrence of each ‘letter’.
toString: produces the text “(CONTAINS <LETTERS>)” where <LETTERS> is the ‘letters’ string.
getLetters(): this method must return letters.
equals(Object): careful on this one. Two Contains are equal if they have the same letters (order is not relevant).
(Example):

Pattern p = new Contains(“re”);
boolean f1 = p.matches(“renew”); // f1 is true
boolean f2 = p.matches(“zoo”); // f2 is false
String s = p.toString(); // s is “(CONTAINS re)”
boolean f3 = p.equals(new Contains(“er”)); // f3 is true.. really..

View Replies View Related

Triangle Pattern (For Loop)

Feb 21, 2015

I'm trying to make a triangle which should look like this. But I cant seem o figure it out.

1
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1

This is the code I have written so far.

public class TestProgram
{
public static void main(String[]args)
{
for (int columns=0; columns<=8; columns++)
{
for (int rows=columns; rows>=1; rows --)
{
System.out.print(rows+ " ");
}
System.out.println();
}
}
}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved