Welcome to Dream.In.Code
Become a Java Expert!

Join 150,151 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,368 people online right now. Registration is fast and FREE... Join Now!




DATA STRUCTURE

2 Pages V  1 2 >  
Reply to this topicStart new topic

DATA STRUCTURE, lab 3

redzuan
28 Jul, 2008 - 04:27 AM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
This my new lab session. Hope we can solve it togetherAttached File  Lab_3.doc ( 38.5k ) Number of downloads: 31

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: DATA STRUCTURE
28 Jul, 2008 - 04:59 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,165



Thanked: 45 times
Dream Kudos: 125
My Contributions
QUOTE(redzuan @ 28 Jul, 2008 - 05:57 PM) *

This my new lab session. Hope we can solve it togetherAttached File  Lab_3.doc ( 38.5k ) Number of downloads: 31




Sure, but you need to post whatever you have tried till now. That will help us to help you better. smile.gif
User is online!Profile CardPM
+Quote Post

redzuan
RE: DATA STRUCTURE
29 Jul, 2008 - 02:53 AM
Post #3

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
Here is my code but there is still logic error. Still confuse??
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package numtonames;
import java.util.Scanner;
/**
*
* @author Mohd Redzuan Omar
*/
public class NumToNames {

       static final String[] number1 ={"","Hundred"};
       static final String[] number2 ={ "zero", "one", "two", "three", "four", "five", "six"
                , "seven", "eight", "nine", "ten"};
  
    
    
     public static String numToNames(int n)
    {
       String str;
       if(n %100 < 10)
       {
          str = number2[n % 100];
          n/=100;//n=n/100
       }
       else
       {
            str = number2[n % 5];
            n/=5; //n=n/5
       }
        if(n == 0) return str;
        return number2[n] + " hundred " + str;
        
        
        
    }
    
    public String convert(int number)
    {
        if(number == 0)
        {
            return "zero";
            
        }
        
        String pra = "";
        String str1 = "";
        
        int i = 0;
        do{
            int n = number % 100;//example if 372%100 =72
            if(n!=0)// if n bukan 0
            {
                String s = numToNames(n);//call method numToNames
                str1 = s + number1[i] + str1;
            }
            i++;//increase i by 1
            number = number / 100;
        }while(number > 0);
        return (pra+str1).trim();//trim (edit space)
    }

    public static void main(String[] args) {
        NumToNames number = new NumToNames();
        Scanner scanner = new Scanner (System.in);
        System.out.println("Enter a positive integer: ");
        int num = scanner.nextInt();
        
        if(num > 0)
        {
            
            System.out.println(number.convert(num));
        }
        else if(num < 0)
        {
            System.out.println("Invalid number, try again");
        }
    }

}


This post has been edited by redzuan: 29 Jul, 2008 - 03:04 AM
User is offlineProfile CardPM
+Quote Post

vik09
RE: DATA STRUCTURE
29 Jul, 2008 - 10:18 AM
Post #4

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 32


My Contributions
What about something like this:
convert the entered number to a String and put every char(digit) it to a char[]. Something like this:
String str =Integer.toString(num);
char[] c = str.toCharArray();
And then with a for cycle, curcle through the chars and with sitch print every char(digit) with the corresponding name.

Sorry for the mess but I don`t have a compiler on the computer to give you a better example.
User is offlineProfile CardPM
+Quote Post

redzuan
RE: DATA STRUCTURE
29 Jul, 2008 - 09:23 PM
Post #5

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
QUOTE(vik09 @ 29 Jul, 2008 - 11:18 AM) *

What about something like this:
convert the entered number to a String and put every char(digit) it to a char[]. Something like this:
String str =Integer.toString(num);
char[] c = str.toCharArray();
And then with a for cycle, curcle through the chars and with sitch print every char(digit) with the corresponding name.

Sorry for the mess but I don`t have a compiler on the computer to give you a better example.


please explain more i did not get u.
User is offlineProfile CardPM
+Quote Post

vik09
RE: DATA STRUCTURE
29 Jul, 2008 - 11:17 PM
Post #6

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 32


My Contributions
I`m thinking of something like this:
CODE
        //the entered number
        int i = 63546;
        String str = Integer.toString(i);
        char[] c = str.toCharArray();
        for (int j = 0; j <= c.length - 1; j++) {
            int a = Integer.parseInt(Character.toString(c[j]));
            switch (a) {
            case 1:
                System.out.print("One ");
                break;
            case 2:
                System.out.print("Two ");
                break;
            case 3:
                System.out.print("Three ");
                break;
            case 4:
                System.out.print("Four ");
                break;
            case 5:
                System.out.print("Five ");
                break;
            case 6:
                System.out.print("Six ");
                break;
            case 7:
                System.out.print("Seven ");
                break;
            case 8:
                System.out.print("Eight ");
                break;
            case 9:
                System.out.print("Nine ");
                break;
            case 0:
                System.out.print("Zero ");
                break;

            }
        }
    }

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: DATA STRUCTURE
29 Jul, 2008 - 11:25 PM
Post #7

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,165



Thanked: 45 times
Dream Kudos: 125
My Contributions
I think you made it bit more complex that expected. When value is 372, you are supposed to print "Three Seven Two" and not "Three hundred Seventy two". So I think this should be enough.

java


public static String numToNames(int n)
{
String str = "";
int digit;

if(n<10)
{
digit = n;
}
else
{
digit = n%10;
str.concat(numToNames(n/10));
}
switch(digit)
{
case 0: str.concat("Zero ");
break;
case 1: str.concat("One ");
break;
case 2: str.concat("Two ");
break;
case 3: str.concat("Three ");
break;
case 4: str.concat("Four ");
break;
case 5: str.concat("Five ");
break;
case 6: str.concat("Six ");
break;
case 7: str.concat("Seven ");
break;
case 8: str.concat("Eight ");
break;
case 9: str.concat("Nine ");
break;
}

return str;

}



This code might have some syntax errors as it is not compiled, verify it by yourself.

I hope this will help you. smile.gif



--EDIT--
vik09, your solution will work, but it is not what the assignment is expecting. The function needs to be the recursive function, as per the assignment instructions.

This post has been edited by AmitTheInfinity: 29 Jul, 2008 - 11:29 PM
User is online!Profile CardPM
+Quote Post

vik09
RE: DATA STRUCTURE
29 Jul, 2008 - 11:42 PM
Post #8

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 32


My Contributions
I was just giving him the idea. He needs to make the whole thing work. smile.gif

This post has been edited by vik09: 29 Jul, 2008 - 11:42 PM
User is offlineProfile CardPM
+Quote Post

redzuan
RE: DATA STRUCTURE
30 Jul, 2008 - 01:03 AM
Post #9

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
QUOTE(vik09 @ 30 Jul, 2008 - 12:42 AM) *

I was just giving him the idea. He needs to make the whole thing work. smile.gif

yeah suppose it recursive type.
User is offlineProfile CardPM
+Quote Post

redzuan
RE: DATA STRUCTURE
31 Jul, 2008 - 08:34 PM
Post #10

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
this my answer for question 2(a) and (cool.gif for this lab. for ques 1 still working about it:
2(a)
CODE
package palindromes;
import java.util.Scanner;
/**
*
* @author Mohd Redzuan Omar
*/
public class CheckPalindrome {

    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a string: ");
        String str = scanner.next();
        if(isPal(str))
        {
            System.out.println( str + " is a palindrome");
        }
        else
            System.out.println( str + " is not a palindrome");
    }
    public static boolean isPal(String s)
    {
        return isPal(s,0,s.length()-1);
    }
    public static boolean isPal(String str, int startIndex, int endIndex)
    {
        
        
        if(endIndex <= startIndex )
        {
            
        }
        else if(str.charAt(startIndex) != str.charAt(endIndex))
        {
            return false;
            
        }
        else
        return isPal(str,startIndex+1,endIndex-1);
        return true;
      
    }
}


answer for 2(cool.gif
CODE
package palindromes;
import java.util.Scanner;
/**
*
* @author Mohd Redzuan Omar
*/
public class CheckPalindromes {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] str = new String[5];
        int i;
        System.out.println("Enter a string: ");
        for( i=0; i<str.length;i++)
        {  
            str[i]=scanner.next();
            if(isPal(str[i]))
            {
                System.out.println( str[i] + " is a palindrome\n");
            }
            else
            System.out.println( str[i] + " is not a palindrome\n");
        }
        
        
    }
    public static boolean isPal(String s)
    {
        return isPal(s,0,s.length()-1);
    }
    public static boolean isPal(String str, int startIndex, int endIndex)
    {
        
        
        if(endIndex <= startIndex )
        {
            
        }
        else if(str.charAt(startIndex) != str.charAt(endIndex))
        {
            return false;
            
        }
        else
        return isPal(str,startIndex+1,endIndex-1);
        return true;
      
    }

}


hey how to turn off the smiley face ? where i can turn it off
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: DATA STRUCTURE
31 Jul, 2008 - 09:04 PM
Post #11

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,165



Thanked: 45 times
Dream Kudos: 125
My Contributions
you are still working on question 1? what problem you are facing there now?
User is online!Profile CardPM
+Quote Post

pbl
RE: DATA STRUCTURE
31 Jul, 2008 - 09:15 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
what about replacing

CODE

       switch(digit)  
          {  
              case 0: str.concat("Zero ");  
                         break;  
              case 1: str.concat("One ");  
                         break;  
              case 2: str.concat("Two ");  
                         break;  
              case 3: str.concat("Three ");  
                         break;  
              case 4: str.concat("Four ");  
                         break;  
              case 5: str.concat("Five ");  
                         break;  
              case 6: str.concat("Six ");  
                         break;  
              case 7: str.concat("Seven ");  
                         break;  
              case 8: str.concat("Eight ");  
                         break;  
              case 9: str.concat("Nine ");  
                         break;  
          }  


by

CODE

final String[] strDigit = {"Zero", "One", "Two", "Three",....};

str.concat(strDigit[digit]);  


User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month