Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2004, 02:25 AM   PM User | #1
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
Homework help desperate!!!!

I am trying to get this project done for my CS class but I don't know if I am anywhere in the ball park. THis is what I have so far. I need to have a toDecimal, toBinary, and isBinary, that return true for a legal binary number and false other wise. Could someone take a look at this and see if they could help?

PHP Code:
 import java.io.*;  
public class 
BinConverter  
{  
    public static 
void main(String[] args)  

 
System.out.println("Welcome to your third java program!!"); 
//next 2 lines are the setup for reading stuff in from the user 
InputStreamReader input = new InputStreamReader(System.in);  
BufferedReader console = new BufferedReader(input);  
String line "s";  
if (
isBinary (line))  
{  
    
int numtoDecimal(s);  
    
System.out.println(num);  
    
String answer=toBinary (num);  
    
System.out.println (answer); 
     
    
}  
else  
{  
    
System.out.println("Not Binary");  
}  
public static 
boolean isBinary (String s

  
int l s.length();  
  
int check 1
  for (
int i 0li++) 
  {    
    if (
s.charAt(i) == '1' || s.charAt(i) == '0'
    { 
     else 
    }  
check 0
   }  
  if (
check == 0
    return 
false
 
  else 
    return 
true
}  
public static 
double toDecimal (String s
  { 
    
int l s.length(); 
    
double result 0
    
System.out.println("s=" s); 
    
System.out.println("l=" l); 
    for (
int i 0li++) 
    {  
      
System.out.println("result before iteration=" result); 
      
System.out.println("s.charAt(i)=" + (double)s.charAt(i)); 
      
System.out.println("s.length() - i - 1=" + (s.length() - 1)); 
      
System.out.println("Math.pow(2, (s.length() - i - 1))=" Math.pow(2, (s.length() - 1))); 
      
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - 1))) + " to result"); 
      
result result s.charAt(i) * Math.pow(2, (s.length() - 1)); 
      
System.out.println("result after iteration=" result); 
    } 
    
System.out.println("final result=" result); 
    return 
result
 
  } 
 
 
public static 
String toBinary (int n)  
{  
if (
2==0)  
return 
0;  
n--;  
}  
else  
{  
return 
1;  
n--;  
n;  
}  
}  

I have two errors: 25 : illegal start of expression static boolean isBinary (String s)
78 : ';' expected }

Last edited by xellos16; 12-04-2004 at 05:15 AM..
xellos16 is offline   Reply With Quote
Old 12-04-2004, 02:48 AM   PM User | #2
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
Personally i wouldn't give a variable a value of 0 if it was false. I'd just use a bool and say that once it has reached a character which isn't a 0 or a 1 then change the boolean value to false so it would return false. That should cut down the program quite a lot.

I'm not sure but i think you may have to add another modifier to your isBinary method, public static instead of just static. Not sure on that one though.

Code:
public static boolean isBinary(String s)
{
  boolean check = true;
  for (int i = 0; i < s.length(); i++)
  {   
    if (s.charAt(i) != '1' && s.charAt(i) != '0')
    {
          check = false;
    }
  }
       return check;
}
I haven't got a clue if that's right, i'm first year CS as well but that's just an idea as it cuts down the code.

EDIT: It's just a way to cut down your code, if the if statement fails each time then it won't change the value of the variable check to false meaning that the value IS binary. Flame me if you will, it was just an idea

EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!)

Last edited by KeZZeR; 12-04-2004 at 03:08 AM..
KeZZeR is offline   Reply With Quote
Old 12-04-2004, 03:46 AM   PM User | #3
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
A hint to help you with your problem. You have methods inside methods.
turbowrx is offline   Reply With Quote
Old 12-04-2004, 04:16 AM   PM User | #4
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by KeZZeR
EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!)
Well, there's a difference: The unwritten rule is: We don't WRITE code for homework assignments. However, this is quite another case. He has evidently tried to make it work, he gets an error, and wants to find out what he's doing wrong. Telling him what he should think of and what mistakes he's making will help him become a better programmer. It's those that expect us to write the program for them that we don't help. Those that are really trying, those we do help.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 12-04-2004, 04:21 AM   PM User | #5
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
methods inside methods? How?
xellos16 is offline   Reply With Quote
Old 12-04-2004, 04:27 AM   PM User | #6
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
Well, your main method seems to encompass your other methods, for example the isBinary method.
turbowrx is offline   Reply With Quote
Old 12-04-2004, 04:29 AM   PM User | #7
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
There seems to be other brackets in need of fixing as well.
turbowrx is offline   Reply With Quote
Old 12-04-2004, 04:31 AM   PM User | #8
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
And what is that while loop doing at the end of your code?
turbowrx is offline   Reply With Quote
Old 12-04-2004, 04:31 AM   PM User | #9
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
Do you think that that my be due to a missed bracket somewhere, or did I just screw up something?
xellos16 is offline   Reply With Quote
Old 12-04-2004, 04:33 AM   PM User | #10
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
You also have a problem in your main method with the variable s. You never create it.

You also have a duplicate num variable.

Well, it looks like you may have misplaced the bracket at the end. Just create and delete brackets where you need them.

There are more too. You'll come up on them as you get the bracket situation fixed. Just work your way through it and I'll help you if you need it.

Looks like you're going to need some work on your toDecimal method as well.

Last edited by turbowrx; 12-04-2004 at 04:44 AM..
turbowrx is offline   Reply With Quote
Old 12-04-2004, 04:51 AM   PM User | #11
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
I deleted the num varible at the top because I don't think I need it For the toBinary statement. The while loop at the bottom goes to the string toBinary statement as well. Do I need to change the variable 's' from a 2 of the statments. And I think I called the 'S' statment at the top, I am probably wrong though.
xellos16 is offline   Reply With Quote
Old 12-04-2004, 05:02 AM   PM User | #12
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
I'm thinking maybe your s variable in main should be line, and you just put s because that is what you set line to. You are trying to send s to the isBinary method, but there is no s to send.

I don't see where the while loop at the bottom does anything.
turbowrx is offline   Reply With Quote
Old 12-04-2004, 05:16 AM   PM User | #13
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
I updated the file. How do I get rid of that illegal start? ANd a tthe buttom of the page, it says that I am missing a semi-colon. How do I check for that?
xellos16 is offline   Reply With Quote
Old 12-04-2004, 05:29 AM   PM User | #14
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
I think your original 2 errors had to do with the fact that your main method was bracketed correctly, which is what the illegal start is. You can't call the isBinary method if it's not outside the main method. Your main method needs to end.

Why don't you repost your code as well.
turbowrx is offline   Reply With Quote
Old 12-04-2004, 05:32 AM   PM User | #15
xellos16
New Coder

 
Join Date: Dec 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
xellos16 is an unknown quantity at this point
Ok, I did that. This is where I am at now.

PHP Code:
 import java.io.*;  
public class 
BinConverter  
{  
    public static 
void main(String[] args)  

 
System.out.println("Welcome to your third java program!!"); 
//next 2 lines are the setup for reading stuff in from the user 
InputStreamReader input = new InputStreamReader(System.in);  
BufferedReader console = new BufferedReader(input);  
String line "s";  
if (
isBinary (line))  
{  
    
int numtoDecimal(s);  
    
System.out.println(num);  
    
String answer=toBinary (num);  
    
System.out.println (answer); 
     
    
}  
else  
{  
    
System.out.println("Not Binary");  
}  
}
public static 
boolean isBinary (String s

  
int l s.length();  
  
int check 1
  for (
int i 0li++) 
  {    
    if (
s.charAt(i) == '1' || s.charAt(i) == '0'
    {} 
     else 
      
check 0
   }  
  if (
check == 0
    return 
false
 
  else 
    return 
true
}  
public static 
double toDecimal (String s
  { 
    
int l s.length(); 
    
double result 0
    
System.out.println("s=" s); 
    
System.out.println("l=" l); 
    for (
int i 0li++) 
    {  
      
System.out.println("result before iteration=" result); 
      
System.out.println("s.charAt(i)=" + (double)s.charAt(i)); 
      
System.out.println("s.length() - i - 1=" + (s.length() - 1)); 
      
System.out.println("Math.pow(2, (s.length() - i - 1))=" Math.pow(2, (s.length() - 1))); 
      
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - 1))) + " to result"); 
      
result result s.charAt(i) * Math.pow(2, (s.length() - 1)); 
      
System.out.println("result after iteration=" result); 
    } 
    
System.out.println("final result=" result); 
    return 
result
 
  } 
 
 
public static 
String toBinary (int n)  
{  
if (
2==0)  
return 
0;  
s--;  
}  
else  
{  
return 
1;  
s--;  
2;  
}  

With these two errors
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:71: illegal start of type
else
^
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:76: <identifier> expected
}
xellos16 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:10 AM.


Advertisement
Log in to turn off these ads.