Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A hint to help you with your problem. You have methods inside methods.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #5
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    methods inside methods? How?

  • #6
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, your main method seems to encompass your other methods, for example the isBinary method.

  • #7
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There seems to be other brackets in need of fixing as well.

  • #8
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And what is that while loop doing at the end of your code?

  • #9
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you think that that my be due to a missed bracket somewhere, or did I just screw up something?

  • #10
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #11
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #12
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #14
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #15
    New Coder
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    }


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •