7 Replies - 377 Views - Last Post: 06 October 2012 - 02:58 PM Rate Topic: -----

#1 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Lost on where to start on string alteration

Posted 05 October 2012 - 08:40 PM

Hi again folks, I am looking for a nudge in the right direction on where to start with my current assignment. In my assignment I was given a UML and I have to create a program that will take entered text and alter it. Here is the UML:

Posted Image


Here are things I believe I know about the UML:
"WordAnalyzer" is the name of the class
Private values have the "-" before them.
Public values have the "+" before them.


After that I am confused as to where I have to start. I have created a new class that is titled WordAnalyzer, but where do I begin?

Will I have multiple classes for this project or will it all fall under the WordAnalyzer class?

Is This A Good Question/Topic? 0
  • +

Replies To: Lost on where to start on string alteration

#2 accexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

Re: Lost on where to start on string alteration

Posted 05 October 2012 - 09:53 PM

Hi,

I can tell you how I would make this program:
I will make a class, WordAnalyzer (like you did) and here I'll make some methods:
1. ArrayList<String> splitUml (String umlString) which will take as a parameter the umlString to analyze and
return an ArrayList like this:
arrayList[0] = "+getTExt():String"
arrayList[1] = "+setText(String text):void"
etc.
2. ArrayList<String> analyzePiece(String value) which will take as parameter a String from the ArrayList returned by splitUml method and analize it. Will return something like this:
arrayList[0] = "+" //access modifier
arrayList[1] = "getText" //method name
arrayList[2] = "String" //return parameter
Here you can make any logic you like to analyze different uml string types
3. Loop on every item in ArrayList returned by splitUml and call analyzePiece with the item as parameter
4. Now you will have the uml split into pieces and you can change if you like.
5. You can use the array's again to rebuild the uml string (after you make changes in array you loop in reverse order like I show it above but this time you need method which will have as parameter an ArrayList<String> and return an String)

You can use a different type of structure for return type (maybe a LinkedHashList) but this it's up to you.
Of course, if you need a more complex solution it is best to make a separate class for every item in uml (e.g. method description) and share a common interface between elements. But I think you specified that this is just a simple assignment.

Hope this will help you.

Chris

Erata: sorry, I mean "LinkedHashMap" not "LinkedHashList". :)
Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Lost on where to start on string alteration

Posted 05 October 2012 - 10:06 PM

This is very simple. This is a UML representation of ONE class. You will only need one class.
If gives you the name of the class WordAnalyzer. The rest of the UML just tells you what variables and methods you need. It also tells you what parameters for these methods you need, and what they return.

You are correct however. A (-) represents private, and a (+) is public.
Lets start with the class.

public class WordAnalyzer {

}



Now the next part of the UML says
-text: String



This is how UML represents a global variable. You can see by the minus that it is private. Then right after the minus is the name of the variable. Followed by a colon and then followed by the type of variable it is. In this case a String.
This is just telling you to create a private global String named test.

The rest of the lines are methods (notice the line that seperates variable declarations and methods. The top is always for variables the bottom is always for methods.)

The next line is
+WordAnalyzer(text: String)



So you will notice this method has the same name as the class. This means this method is a constructor. You can notice by the plus sign that it is a public constructor. Inside the () characters are what this constructors parameters are.
This variable is in the same format as the last one. the name (text) followed by its type (String).

Lets move on to the next method.
+getText(): String



So again. The + sign tells us this is a public method. The method name is getText, and there is nothing between the () characters which means there are no parameters. After each method it will tell you what this method needs to return. In this case String.

You can do this all the way down until you are done. Then you can start filling in the code. Just ask if you need anymore help from there.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1412
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Lost on where to start on string alteration

Posted 06 October 2012 - 05:47 AM

Just a correction to what Kinaces wrote:

"-text: String" is not a global variable. Instead it is a private attribute (private instance variable in java)

Class or static attributes are underlined :)
Was This Post Helpful? 0
  • +
  • -

#5 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Lost on where to start on string alteration

Posted 06 October 2012 - 06:28 AM

Alright. I've got a start on where to go, how do I properly get text from my main.java into my wordanalyzer class?

I have this line here
// Returns the text
public String getText() {} // end getText()


I believe I have to put something in the {} to pull text from the main.java, but I don't know what it I have to do.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1412
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Lost on where to start on string alteration

Posted 06 October 2012 - 06:40 AM

View PostJakey, on 06 October 2012 - 01:28 PM, said:

Alright. I've got a start on where to go, how do I properly get text from my main.java into my wordanalyzer class?

I have this line here
// Returns the text
public String getText() {} // end getText()


I believe I have to put something in the {} to pull text from the main.java, but I don't know what it I have to do.


The class diagram tells you that you will need to have a private String text variable.
A get method named getText() seems very likely to return the text variable
Was This Post Helpful? 0
  • +
  • -

#7 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Lost on where to start on string alteration

Posted 06 October 2012 - 02:38 PM

I went to work, but have still been thinking about what I have to do here.

Here is my current code:
/*
 * Jacob
 */
package p04classdefinition;


public class WordAnalyzer {
    String text;
    
// Initializes the field text with the parameter text.
public WordAnalyzer(String text) {} // end WordAnalyzer

// Returns the text
public String getText() {} // end getText()


Now I have the getText method, but what do I do with it? Does this pull text from the main.java? At it's current state, I am receiving an error "Missing Return Statement"
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1412
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Lost on where to start on string alteration

Posted 06 October 2012 - 02:58 PM

View PostJakey, on 06 October 2012 - 09:38 PM, said:

I went to work, but have still been thinking about what I have to do here.

Here is my current code:
/*
 * Jacob
 */
package p04classdefinition;


public class WordAnalyzer {
    String text;
    
// Initializes the field text with the parameter text.
public WordAnalyzer(String text) {} // end WordAnalyzer

// Returns the text
public String getText() {} // end getText()


Now I have the getText method, but what do I do with it? Does this pull text from the main.java? At it's current state, I am receiving an error "Missing Return Statement"


I think you need a better understand of methods :)
Go and find a tutorial which explains it in a way you understand, I will just try to explain the basics in a very short way

When defining a method, there are four parts you should worry about
The first part is the access modifier. The access modifier tells which classes are allowed to call the method. The modifiers can be found here

The second part is the return type. A return type of String means that the method should in all paths of it's code return a String object. A method with return type 'void' will not anything.

The third part is the name, in your case it is getText

The last part is the parameters. A method is able to have none or several parameters. You are able to supply values in these parameters when the method is called from elsewhere in your program. Your getText method has no parameters
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1