• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can somebody help to Simplify the following method?  RSS feed

 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody help to Simplify the following method? Thanks!





 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

you should always UseCodeTags (<-- click) when posting code on the forum. I've added them for you this time, see how much better it looks?

Before you start trying to simplify the method, you should take some time to make it at least compile, because currently it does not. Once you've done that there's a whole bunch of suggestions in this other thread from 7 years ago (which I assume is where you got this code from in the first place) that you could consider.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Thank you for replying. This was giving to as a test and could not solve. That is why I want to know how you guys will go about such a case.
I follow the old posts and trying to compile them but none of them work or compiles also.

Somebody must come to my aid please ?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So first things first, find out why it doesn't compile. What is the compiler telling you when you try and compile it?
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Here is what the compiler says:

prog.cs(7,2): error CS1525: Unexpected symbol `public'
prog.cs(7,30): error CS1525: Unexpected symbol `('
prog.cs(7,46): error CS1525: Unexpected symbol `bool'
prog.cs(7,60): error CS1525: Unexpected symbol `bool'
prog.cs(17,16): error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
prog.cs(17,30): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
prog.cs(19,36): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
prog.cs(21,16): error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
prog.cs(23,35): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
prog.cs(25,8): error CS1525: Unexpected symbol `}'
Compilation failed: 10 error(s), 0 warnings
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Thank you for the reply. When I run in the C environment to this what I get.

Compilation error time: 0 memory: 0 signal:0
prog.c:2:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'static'
public static int jabberwock(bool frumious, bool manxome, bool uffish)
^
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ama Ansah wrote:I follow the old posts and trying to compile them but none of them work or compiles also. Somebody must come to my aid please ?


Start from line 1.
Check what type of parameters do you expect to be supplied?
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vilda,
Thank you for your reply. The question only say. Simplify the following?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused about what language you are writing this in.

You reference a file called "prog.cs" which suggests C#. Then you reference a file called "prog.c" which suggests C. But you have posted your question in the Beginning Java forum.

Some clarification about your language choice is required. What language are you trying to use?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also:
Ama Ansah wrote:This was giving to as a test
by whom? We require you to always QuoteYourSources here on the Ranch (it's a copyright thing)
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

I am changing the bool to boolean in the Java compiler too but that one still not compiling. This is how the error from the java compiler as well:


Compilation error time: 0 memory: 0 signal:0
Main.java:12: error: illegal start of expression
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: illegal start of expression
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: ';' expected
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: '.class' expected
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: ';' expected
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: <identifier> expected
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: not a statement
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
Main.java:12: error: ';' expected
public static int jabberwock(boolean frumious, boolean manxome, boolean uffish)
^
8 errors

 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ama Ansah,

You may want to have a look at this > HowToAskQuestionsOnJavaRanch?
I'm sure it would simplify the way to get answers.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

Just new here. so still learning. I hope I am forgiving if I am not able to ask the question effective way.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ama Ansah wrote:Hi Folks,

Just new here. so still learning. I hope I am forgiving if I am not able to ask the question effective way.


Of course you are, unless it becomes a bad habit

Welcome to the Ranch
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

It was in a form of an interview and could not figure it out in a best way. That is why I want to see how you guys will go about it. My answer was this:

public static int jabberwock(bool frumious, bool manxome, bool uffish)
{

if((manxome && uffish) && !frumius)
return JUBJUB_BIRD;
}
else if(( !frumius && !manxome) && uffish) return BANDERSNATCH;
{
else if(( !frumius && !manxome) && !uffish) return BANDERSNATCH;
}

return JUBJUB_BIRD;

}
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should write out a truth table on paper for the three boolean variables.

From that, you can find out how to rewrite it in a simpler way.

If that's the complete code, then you will indeed get compile errors, because it's not a complete program, just a fragment. Methods must be declared inside classes. You can't have a method definition outside of a class.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still unsure what programming language we're dealing with here! So far we've seen a mix of Java, C#, and C. Perhaps I could suggest a solution in Elixr?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not valid C because there is no public keyword in C.
It's not valid Java because there is no bool keyword in Java (it should be boolean).
It could be valid C#, but also in C# this is not a complete program, because just like in Java you can't have methods outside of classes.

The values JUBJUB_BIRD and BANDERSNATCH are also not defined anywhere, because this is not a complete program.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,
It could be valid C#, but also in C# this is not a complete program, because just like in Java you can't have methods outside of classes.

The values JUBJUB_BIRD and BANDERSNATCH are also not defined anywhere, because this is not a complete program.

I think it could be C# as well. I was just as to simplify it !
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot simplify it, if it does not work to begin with.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

So Tim, do you think that could had been a good answer to supply an interviewer?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it were true, that the interviewer gave you incomplete or incorrect code, then yes. But in this case I suspect it is more likely that you are misinterpreting the question when relaying it to the forum.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

This is the exact question:

Simplify the following method


public static int jabberwock(bool frumious, bool manxome, bool uffish)
{
if (!frumious)
{
if (manxome)
{
if (uffish)
{
return JUBJUB_BIRD;
}
else if (!manxome)
{
return BANDERSNATCH;
}
else
{
return JUBJUB_BIRD;
}
}
else
{
return JUBJUB_BIRD;
}
}
else if (!uffish)
{
if (!manxome)
{
return BANDERSNATCH;
}
}
else if (!manxome)
{
return JUBJUB_BIRD;
}
if (!(uffish || !manxome))
{
return BANDERSNATCH;
}
return JUBJUB_BIRD;
}
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're left to guess what language it is? Weird.

What job position was the interview for? Java? C#?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the language and the fact that it won't compile "as is" is irrelevant. I would just go by the given conditionals and structure. A truth table would help simplify this. Perhaps even a Karnaugh Map. But some analysis would reveal that at least one return path is unreachable and you could probably invert some of the checks to remove negations.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
Well for language only C# is mention plus others but not Java...
Somebody was trying to me make me think behind the box.

Now am gradually getting it. Thank you Tim.....


 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucar,

I used the decision table and since is three the possible combination is 8. But still my answer wasn't right and that is why I want to see how best to simplify it.

Thanks
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an interview question, the most likely goal of the question was to see what kind of knowledge you have about boolean arithmetic simplification and your ability to analyze complicated constructs like that given. This is something you learn, or should have learned, in school as part of a Computer Science program. I doubt it had much to do with your ability to think outside, not behind, the box.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first off, there are only three two possible return values so I would probably look for only three two conditional branches, at most. I think you have more than that in your solution and you don't cover every case because you end with an else-if, leaving one condional path uncovered by a return value. You should end with an else-if-else or an else-if followed by an unconditional return to cover the default case.

(Edit - only two possible return values after all}
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lacar,

Check this answer Michael gave in some years ago here:

public static int jabberwock(boolean frumious,boolean manxome,boolean uffish)
{
if ((!frumious && (manxome && !manxome)) || (!uffish && !manxome))
{
return BANDERSNATCH;
}
return JUBJUB_BIRD;
}


It seems he did a great job base on what you are saying!
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ama Ansah wrote:
if ((!frumious && (manxome && !manxome)) || (!uffish && !manxome))
{
return BANDERSNATCH;
}
return JUBJUB_BIRD;
}

It seems he did a great job base on what you are saying!

Maybe, but still no cigar from me. I haven't check this solution for validity but just looking at it, can you think of a way to simplify the boolean expression (manxome && !manxome)?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will probably get more out of this if you post your decision table and explain the steps you took to simplify the logic. Then we can get a better idea of your thought process and give you more specific guidance.
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lular,

here is my table

Frumious Manxome Uffish
1 T T T
2 T T F
3 T F T
4 T F F
5 F T T
6 F T F
7 F F T
8 F F F
 
Ama Ansah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ama Ansah wrote:Can somebody help to Simplify the following method? Thanks!

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how exactly did you use your table to simplify the boolean logic in that code?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take Junilu's point, I am probably being a bit too difficult with the language thing. As an exercise in boolean logic it is not particularly important (although it would be useful for automating your testing and verifying your eventual simplification does not change the function's behaviour).

I see in your previous post that you have drawn out the template of a truth table (0 = false, 1 = true)

F M U
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1


The next step is to evaluate the input conditions required for each return statement to be evaluated. So for the first return statement on line 9 you can follow the condition statements to determine that the inputs need to be !frumious && manxome && uffish. So enter this into your table:


F M U
0 0 0
0 0 1
0 1 0
0 1 1 JUBJUB_BIRD
1 0 0
1 0 1
1 1 0
1 1 1


Then move onto the next return statement on line 13 and populate your table. Repeat for all return statements, and you will then have a completed table showing the output for each combination of inputs.

See how you get on with that, then we can discuss what to do with that information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!