• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOPs based question - Reuse code  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1. you have following keywords If and else then or

Replace keywords with [blue]keyword[blue] from any input string.

for example :
input for program : "If you are good and honest,then you will succeed else not"

expected output :

"[blue]If[blue] you are good [blue]and[blue] honest,[blue]then[blue] you will succeed [blue]else[blue] not"

i did it using string tokenizer but they wanted to use OOPS concept and reuse the code in Q2.

Q2.keywords if : red
and:green
else:blue
then:red
or:green


now replace keyword with respective color on same input string.
expected output

"[red]If[red] you are good [green]and[green] honest,[red]then[red] you will succeed [blue]else[blue] not"

Q3. keywords
if : red : capital
and : green : lower
else : blue : italic
then:red capital
or : green:capital


now replace keyword with respective color and make them capital or lower or italic. "[red][capital]If[capital][red] you are good [green][lower]and[lower][green] honest,[red][capital]then[capital][red] you will succeed [blue][itaclic]else[itaclic][blue] not"

Q4.
keywords if : red : capital:bold
and : green : lower:normal
else : blue : italic:bold
then:red: capital:bold
or : green:capital:normal


now replace keyword with respective color and make them capital or lower or italic.
expected output :

"[red][capital][bold]If[bold][capital][red] you are good [green][lower][normal]and[normal][lower][green] honest,[red][capital][bold]then[bold][capital][red] you will succeed [blue][itaclic][bold]else[bold][itaclic][blue] not"

hope you get this. main thing is to reuse code and use object oriented programming concept. Anyidea how to do it in best way?
Make sure that that code gets reuse.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I don't get it, sorry. You will have to explain it a bit better. You will probably find the answer will occur to you as you explain it. It looks rather as though you are supposed to parse a line of text into separate words. Avoid StringTokenizer: if you follow this link and use ctrl‑f‑legacy it tells you what to use instead. Maybe myString.split("\\s+") where \s+ means any number greater than 0 of whitespace characters and \\ means you have to escape the \.
When you have done the splitting, are you supposed to decide whether a word is a keyword and change its colour to blue if it is? Since you are going on about reuse, does that mean you have classes which do much of what you want already?

And welcome to the Ranch
 
Andy Goldawane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell
Thanks for quick response.
I am not expecting logic here.I have written logic using string split.
but for second question how would i map color with keywords?
this whole problem statment is to focus on implementing oops concept and not the logic.

Say i have logic for qs 1 now how would i go for qs 2 ?
I will share logic here in sometime.
 
Andy Goldawane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the logic for Q1.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that String#split is not part of the logic. I woiuld say the logic is
Divide the input into parts
… and String#split is the implementation of it.
Divide is the “what” and String#split is the “how” as found in this FAQ.
The way you wrote that reply makes it sound as if you were conflating what and how. Write down the “what”, which is the logic. Now write down the logic for Q2.

And we will not give you the logic. You will have to show it to us. That way you will develop and refine it. Since Q2 is supposed to be a refinement of Q1, you can get there by refining the logic of Q1. So you start by writing down how you solved or will solve Q1. When you have the logic, it will be easy to find an implementation.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Goldawane wrote:Following is the logic for Q1. . . .
No, that is the implementation. I can see all sorts of things wrong with it, both in style and in design. It looks entirely like procedural coding. You have no class which represents keywords. You have no method which you can properly override.

All right, you can write a method called getExpectedOutput in a subclass, but it will not be an overriding. It will have to be a completely new method because you cannot reuse any of the code in the original getExpectedOutput method.

Write down the logic you are going to use. Also, you obviously know about Lists. What do you know about the other commonly‑used interfaces in the Collections Framework?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!