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

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




Data Structure

 
Reply to this topicStart new topic

Data Structure

redzuan
23 Jul, 2008 - 11:55 PM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
can someone help me give some ideas how to solve this question. ThanksAttached File  Lab_2.doc ( 40.5k ) Number of downloads: 18

User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Data Structure
24 Jul, 2008 - 02:17 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 191



Thanked: 9 times
Dream Kudos: 100
My Contributions
QUOTE(redzuan @ 24 Jul, 2008 - 12:55 AM) *

can someone help me give some ideas how to solve this question. ThanksAttached File  Lab_2.doc ( 40.5k ) Number of downloads: 18



Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Data Structure
24 Jul, 2008 - 04:18 AM
Post #3

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 322



Thanked: 17 times
Dream Kudos: 225
My Contributions
I will just give you small comments on each problem which should give you a start.

Problem 1:
StringTokenizer
String

Problem 2:
Math
User is offlineProfile CardPM
+Quote Post

redzuan
RE: Data Structure
24 Jul, 2008 - 10:13 AM
Post #4

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
Here is my coding for question 1. Can it be more simple rather than this one. Can it be by using split or just replace method.
CODE

public class Main {
    
    public static void main(String[] Args) throws IOException{
      
        System.out.println("Input string: ");
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(Characters.Convert(input.readLine()));


        
    }

}
public class Characters {
    
    public static String Convert(String cString){
        
        cString = cString.replaceAll(" ", "+");
        cString = cString.replaceAll("\t", "+");

        cString = cString.replaceAll("[^a-zA-Z0-9+]", "");
        

        int length = cString.length();
        int count = 0;

        String temp = "";
        
        temp = temp + Character.toUpperCase(cString.charAt(0));



        
        while (count++ < (length-1)){

                if (cString.charAt(count-1) == '+'){

                    temp = temp + Character.toUpperCase(cString.charAt(count));

                }
                else{
                    temp = temp + Character.toLowerCase(cString.charAt(count));

                }
}
        
        
        return temp;
    }

}

User is offlineProfile CardPM
+Quote Post

redzuan
RE: Data Structure
24 Jul, 2008 - 08:43 PM
Post #5

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
this my coding for question 2. Is it correct what the question wants? thanks for helping. what is abstract class and what it use for?
CODE

public class Vector {

    protected double x;
    protected double y;
    protected Vector()
    {
        
    }
   public Vector(double X, double Y)
   {
       x = X;
       y = Y;
   }
  
   public double getX()
   {
       return x;
   }
  
   public double getY()
   {
       return y;
   }
  
   public void setXY(double X, double Y)
   {
     x = X;
     y = Y;
   }
    

}

public class StandardVector extends  Vector {
    
    public StandardVector()
    {
        
    }
    public StandardVector(double X, double Y)
    {
        super(X,Y);
    }
    
    public double magnitude()
    {
        double total, magnitude;
        double X = getX();
        double Y = getY();
        total = Math.pow(x,2)+Math.pow(y, 2);
        return magnitude = Math.sqrt(total);
        
    }

}

public class OtherVector extends Vector {
    
    public OtherVector()
    {
        
    }
    
    public OtherVector(double X, double Y)
    {
        super(X,Y);
    }
    
    public double magnitude()
    {
        double X = Math.abs(getX());
        double Y = Math.abs(getY());
        return x + y;
    }

}
public class Test {

    public static void main (String[] args)
    {
        double x,y;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter value for x:");
        x = scanner.nextDouble();
        System.out.println("Enter value for y:");
        y = scanner.nextDouble();
        
        StandardVector myVECTOR = new StandardVector(x,y);
        
        System.out.println(myVECTOR.magnitude());
        
        OtherVector newVECTOR = new OtherVector(x,y);
        
        System.out.println(newVECTOR.magnitude());
        
        
        
    }
}


This post has been edited by redzuan: 24 Jul, 2008 - 08:44 PM
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Data Structure
25 Jul, 2008 - 05:13 AM
Post #6

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 322



Thanked: 17 times
Dream Kudos: 225
My Contributions
QUOTE(redzuan @ 24 Jul, 2008 - 09:43 PM) *

what is abstract class and what it use for?

Google

This post has been edited by lordms12: 25 Jul, 2008 - 05:13 AM
User is offlineProfile CardPM
+Quote Post

redzuan
RE: Data Structure
29 Jul, 2008 - 03:02 AM
Post #7

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
Still need someone expert to give a comment with the program...need to improve about it either its correct or not
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Data Structure
3 Aug, 2008 - 01:32 AM
Post #8

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 322



Thanked: 17 times
Dream Kudos: 225
My Contributions
Question 1:
A lot of spaces and enters is not comfortable
Using StringBuffer rather than using String concatenation is better
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] Args) throws IOException{
System.out.println("Input string: ");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println(Characters.Convert(input.readLine()));
}
}

java
import java.util.StringTokenizer;

public class Characters {

public static String Convert(String cString){
int index;
StringBuffer strBufer = new StringBuffer();
StringTokenizer strTokenizer = new StringTokenizer(cString);

// To save if in next for
strBufer.append(strTokenizer.nextToken());
index = 0;
strBufer.setCharAt(index, Character.toUpperCase(strBufer.charAt(index)));
strBufer.append("+");
//

int tokensCount = strTokenizer.countTokens();
for(int i = 0; i < tokensCount; i ++){
strBufer.append(strTokenizer.nextToken());
index = strBufer.lastIndexOf("+") + 1;
strBufer.setCharAt(index, Character.toUpperCase(strBufer.charAt(index)));
strBufer.append("+");
}
strBufer.deleteCharAt(strBufer.length() - 1);//remove last +
return strBufer.toString();
}
}


Question 2:
Abstract classes.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:58AM

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