Intrade 33 Junior Poster

It is my understanding that in C++ objects of derived classes invoke the derived class' constructor but before the body is executed the base class' constructor is invoked and takes effect and then the derived class' constructor's body is executed. If this knowledge is correct, then what happens if the base class' constructor initializes a private variable of the base class that would not be inherited to the derived class, when a new objecct of the derived class is declared.

The variable still exists. You'd have to provide a means of access to it either via protected or public mechanisms, or means of altering it from a function declared in the base class that allows you to do so. The access restrictions obviously still apply.

Intrade 33 Junior Poster

Is there a specific reason why you are intermingling C++ and C-like data storage?

If you want to store characters like immutable strings, you should be using const char arrays as opposed to char arrays.

But that's besides the point.

What you're doing is assigning a const char array of a particular size to a character value in the arrays that you declared in class scope.

You can make your life easier with the <string> implementation as opposed to using globally scoped arrays.

Just keep in mind that arrays aren't pointers - arrays are of a particular size and you cannot make them point to a different address. Once you declare an array of a size (say 255), you can't make it point to another array, which is what it looks like you are doing.

You can negate the problem by changing your globally-scoped arrays into pointers (particularly const char pointers) and have them point to c-strings declared in your constructor.

I don't understand regular strings (as opposed to the STL defined strings), but what I've heard is that these strings are 'handled' within read-only memory so for the most part you can trust your implementation to handle these type of strings for you without worrying about allocating memory or freeing it.

I wouldn't place logic on faith. Use the STL and make your life easier and your code portable.

Example:


change

char name[255];

to


       
Intrade 33 Junior Poster

I wrote this program hoping that it would examplify this issue a bit better.

#include <iostream>
#include <ctime>
#include <string>
#include <cmath>

#define OR |
#define NOT ~
#define AND &
#define XOR ^

using std::cout;
using std::endl;
using std::time_t;
using std::cin;
using std::string;

const char* const TRUE = "TRUE";
const char* const FALSE = "FALSE";
const char* toName(bool);
string getBitValue(int);

int main(){
    
    srand( static_cast<unsigned int>(time(0)) );
    
    // input
    int a = rand()%4;
    int b = rand()%5;
    int c = rand()%6;
    
    // showing the values for test-purposes
    cout << "a: " << a << " : " << getBitValue(a) << endl;
    cout << "b: " << b << " : " << getBitValue(b) << endl;
    cout << "c: " << c << " : " << getBitValue(c) << endl;
    cout << "\n\n\n" << endl;
    
    //results
    int a_or_b = a OR b;
    int a_or_c = a OR c;
    int b_or_c = b OR c;
    int a_or_nb = a OR (NOT b);
    int a_or_nc = a OR (NOT c);
    int b_or_nc = b OR (NOT c);
    int a_xor_b = a XOR b;
    int a_xor_c = a XOR c;
    int b_xor_c = b XOR c;
    int a_xor_nb = a XOR (NOT b);
    int a_xor_nc = a XOR (NOT c);
    int b_xor_nc = b XOR (NOT c);
    int a_and_b = a AND b;
    int a_and_c = a AND c;
    int b_and_c = b AND c;
    int a_and_nb = a AND (NOT b);
    int a_and_nc = a AND (NOT c);
    int b_and_nc = b …
Intrade 33 Junior Poster

C++ doesn't have automatic memory management, which is a very big deal. This means it takes a lot of knowledge to use the language efficiently. The language might be tolerable to people who have this knowledge, but not to those who lack it.

The C++ language by itself doesn't have automatic memory management, but that is why libraries exist to allow programmers to be able to use the language without worrying too much about memory.

Depending on your needs, the STL is more than enough. If the STL is too bulky for the platform you're working on, then you should be smart enough to write Pseudo-code and look at documentation to follow when certain objects/types need to be freed. If the bulkiness isn't the problem and the STL doesn't provide you with enough firepower, there's always Boost and a variety of other libraries.

To the Original Poster: I believe the new C++ should be coming out this year (or already has). You should look into it and see if it will be better?

Intrade 33 Junior Poster

Why is it that when I attempt to repaint the entire Applet, the screen flashes (as if the repaint requests are slowed down due to many details being painted all at once), whereas a repaint to a JPanel added to the ContentPane of the JRootPane doesn't flash at all?

Then again, I'm not sure if the problem lies within eclipse or within NetBeans 6.1, although I'm fairly certain they're pointing to the same JRE...

Intrade 33 Junior Poster

To make things more visual to find a solution, you could use concepts of propositions and sets combined, and treat a number as a set of binary values at a particular position.

Using 3, 4 and 5 as examples, where the left point-position is the binary value and the right point-position is the position the value exists for a 'number set'...

a = 3, b = 4 and c = 5.

a = 3 = 011 in binary
b = 4 = 100 in binary
c = 5 = 101 in binary

a = 3 = { (1, 0), (1, 1) }
b = 4 = { (1, 2) }
c = 5 = { (1, 0), (1, 2) }

Before operating on the above values, let's try to make some reasonable deductions on potential cases using familiar binary operations.

If we know the result of a XOR c, then we know that the result is a set that consists of binary elements that exist in either a or c, but not both.

If a AND c result contains no elements (or, its zero), and a OR c is not zero, then neither a nor c have common binary elements. We can assume that the result of a OR c will be either a, c or a number greater than both due to the combination of binary elements for the result.

Assuming that unary operations are acceptable, the …

Intrade 33 Junior Poster

Your list is incorrect, because you assume that t is a pointer of some type, when it might not be.

Consider your own example when you declare a list with the
double type, then this happens--

class list<double> {
	int sz;
	double elem;
public:
        // default constructor attempts to pre-initialize elem with
        // a pointer to a continguous amount of doubles of size 0
        // immediate type-mismatch
	list() : sz(0), elem(new double[0]){ }

        // same problem here
	list(int i) : sz(i), elem(new double[i]){ }

        // same problem here
	list(int* i, double val) : sz(i), elem(new double[i])
	{
                // if sz is 5, and i is 0, when is 0 > 5? never? Will this execute? Maybe you meant i < sz ?
		for(int i=0;i>sz;i++) elem[i] = val;
	}

        // how are you returning a copy of a list-type from a double?
	list operator[](int i){ return elem[i]; }

	int size(void){ return sz; }
};

-- I also commented in some logic-errors I noticed when attempting to do what the compiler does the moment you declare a list<double>

Intrade 33 Junior Poster
struct list {   //your structure
       Shape_data;
       Triangle_data;
       Square_data;
       list *link_front, *line_back
}

list *example;   //initializing your pointer
expample = new struct list;

Not quite sure what you mean by including your class in a structure; honestly I would just make a class that inherits all three but the above code is pretty generic as far as it goes. Functions like a normal linked list. You do not need pointers to the class objects inside the list because they are dynamic because list is a pointer, so you can delete all of them by deleting the pointer, just to keep something to track your list so you can free all the memory if you need to.

First: I personally don't agree with this style. Whenever you declare a list, you immediately need space for all 3 kinds of Shapes. If you want your list to grow, you'd have to declare another list to point to that is also going to take up a lot of space in memory.

Second: The list doesn't have to be a pointer. It can be declared on the stack, but that's beside the point.

Third: I think the user was looking for a container-type that could store any type of the Shape specified and he wanted to avoid datum slicing.


I'm not near a compiler, but a solution to this problem could be something similar to this--

// assuming shape is defined
// intent: doubly-linked list that can contain any …
Intrade 33 Junior Poster

I think he is a jerk. I am really trying to understand what I am doing. You cannot be a good program writer or anything that you do if you cannot have a good understanding of what you are doing.

I've been on these forums for awhile now. Ezzaral has helped countless students and industry programmers with common problems.

The way you say you have been "getting things right" even though you don't understand how leads many to conclude that--

a) You are copying and pasting code given to you by your Instructor and you tinker around with it until your program has enough specs to gain you a passing grade.

b) Your Instructor is far too lenient in passing its students, or

c) The assignments aren't too difficult at all, or

d) You're getting the solutions from an external source (which is what Ezzaral pointed out, except I believe he was somewhat harsh although it could've been worst in a way that one would believe you're cheating your way through these assignments).


In any case, the best way to understand the program is to first understand what your intent is before you program.

Study the assignment's intent, then study the language you intend to you and how you can do certain things.

In my opinion, programming is nothing more than an interface that allows users/people (aka programmers) to interface with a computer. The basic functions of a programming langauge …

Intrade 33 Junior Poster

Posting more than once is counterproductive.

Explicitly declare your doubly-linked list to store references to Shapes.

Intrade 33 Junior Poster

>If my assignment is right let me know but I need someone to explain to me why it is right.
Why? Are you completing them through automatic writing or channeling or something?

Although this was harsh, it was hilarious.

Win.

Intrade 33 Junior Poster

For one, which operations are allowed? You said "any" operation, but are we talking strictly binary, or any kind of operation between a, b and c?

Also notice the immediate difficulty of using XOR alone - if a, b and c are all the same number, how can you possible find the solution? Any number XOR'd with itself will be zero.

Are there restrictions on a, b and c (are they completely different numbers, or is a > b > c or c > b > a or what? ) ?

Intrade 33 Junior Poster

Mind if I ask why you want to create your own Web Browser?

If I ever thought about taking on a project like this, I'd at least look up the definition of what it is I'm trying to make (or remake) if one exists.

http://en.wikipedia.org/wiki/Web_Browser

From that definition alone, you're looking at learning a language of your choice, learning how to access files on the WWW, learning how to interpret tags in files such that a specific action is done, such as placing a particular 'pane' at a particular location in your Browser's document pane.

Even after you access a file, you're going to need to make various "parsers" for different kinds of languages.

I'm sure you would need to know more than this. It doesn't look like an easy task, as one would have to learn the many different mark-up languages first, then finally decide on a suitable language to use, then learn, then finally start making deductions on how to recursively parse and handle data encountered in the document.

If you need to finish this anytime soon, expect sleepless nights! =)

Intrade 33 Junior Poster

You can make a class that handles sentences by accepting String input in the Constructor, storing the original String in a reference variable, then stores the Tokenized-version of the String in another appropriate reference variable then make another String array of equal length of the tokenized on and iterate through, appending to each indice the String located in the same indice of the other tokenized array along with an appended value (based on logic defined in your other topic).

Then make methods to retrieve the values of each (the "Splited" string, the "Tokenized" string, and the "part of Speech" string).

Once you make the class, you can modify the toString() method of the class to return the logic in the right order and write the String to a .txt file.

Then you can just iterate through all of the sentences in your TextArea, use an instance of the class to handle the data, then write the data to the file.

You can even make the class a utility class (no constructor, just static methods that return the sentence in tokenized form or part of speech form) but I don't know how much more or less efficient this will be for your needs.

This is just A solution, it's not the only one. I'm not going to bother writing the code, just giving you an idea of what you can do to handle your data effectively.

Intrade 33 Junior Poster
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String readPOS;

		readPOS = jTextArea1.getText();

		String []POS;
		POS = readPOS.split("[-.!? ]");

        jTextArea2.append("Part of Speech Result:\n");
		for(int i = 0; i < POS.length; i++)
		{
            if (POS[i].equals("makan")||POS[i].equals("kerja"))
            {
                jTextArea2.append((POS[i] + "/KK" + " | "));
            }
            else if (POS[i].equals("sakit")||POS[i].equals("cantik"))
            {
                jTextArea2.append((POS[i] + "/KS" + " | "));
            }
            else if (POS[i].equals("saya")||POS[i].equals("dia"))
            {
                jTextArea2.append((POS[i] + "/KGN" + " | "));
            }
            else if (POS[i].equals("bapa")||POS[i].equals("isteri")
                    ||POS[i].equals("doktor")||POS[i].equals("encik")||POS[i].equals("nasi"))
            {
                jTextArea2.append((POS[i] + "/KNA" + " | "));
            }
            else if (POS[i].equals("Ali")||POS[i].equals("Dr.")||POS[i].equals("En."))
            {
                jTextArea2.append((POS[i] + "/KNK" + " | "));
            }
            else if (POS[i].equals("dan")||POS[i].equals("atau"))
            {
                jTextArea2.append((POS[i] + "/KPA" + " | "));
            }
            else if (POS[i].equals("nya")||POS[i].equals("an")||POS[i].equals("kah"))
            {
                jTextArea2.append((POS[i] + "/KPC" + " | "));
            }
            else if (POS[i].equals("mengapa")||POS[i].equals("adapun")
                    ||POS[i].equals("aduh")||POS[i].equals("sila"))
            {
                jTextArea2.append((POS[i] + "/KPK" + " | "));
            }
            else 
            jTextArea2.append((POS[i] + "/KNK" + " | "));
         }jTextArea2.append("\n\n\n");
         // TODO add your handling code here:
    }

can i simplify (POS.equals("mengapa")||POS.equals("adapun")
||POS.equals("aduh")||POS.equals("sila"))?? because i still have alot of OR need to put in. By tis problem i can use database??? anyone can tell me? thanks.

You can make a private method that returns true if the input is correct, in the same file that your Listener is defined--

private boolean canAppendKPK(String value){
   return ( value.equals("mengapa")||value.equals("adapun")
                    ||value.equals("aduh")||value.equals("sila") );
}

--so the statement can look like this--

// ...
            else if (canAppendKPK(POS[i]))
            {
                jTextArea2.append((POS[i] + "/KPK" + " | "));
            }
// ...

-- then again, you can help us do a better job by explaining what it is your program is trying …

Intrade 33 Junior Poster

I think you need to add a CODE BASE parameter with a value that specifies where your .class file is located.

Without it, I don't think the AppletViewer can locate your .class file. I don't know enough about the JDK to verify this, but I do recall having a similar problem after trying to make an Applet run on a different server.

Intrade 33 Junior Poster

Keep in mind that your vector stores parent objects, and when you
push_back (or add) a child object to the vector, you're assigning
the members that a parent object would know about to some copy that is stored in the vector.

To get the polymorphic behavior that you want, consider changing your vector so it holds pointers to parent types--

include <iostream>
#include <string>
#include <vector>

using namespace std;
#include "example.h"

int main() {

    vector <parent*> a;
    parent h('{');
    child c('x', "test");
    a.push_back(&h);
    a.push_back(&c);

    for (int i = 0; i < a.size(); i++) {
        parent * p = a.at(i);
        p->print();
    }

    return 0;
}
Intrade 33 Junior Poster

Every time I attempt to write a program of my choice, I immediately get a mind block and I'm stuck on the project for awhile not because I don't understand the project, but because I've written so many programs that it has gotten to the point where I don't want to write an incorrect program so I'm stuck writing it on paper. When I put some serious thought into it I make notepad documents with Pseudocode. Then I keep thinking through it and I realize I don't have much of a heart to finish the program, but I know it is good training for me to do so because there are programs that I will work on in the future that I may not like or agree with and I have to get used to fixing them or adding to them, etc.

I have all of these thoughts running in my head and I can't simply just "write code." I know of some people that gain an idea and can immediately design what it is they're thinking with some refactoring done here and there on the way and wallah they have a program. But I'm not at all like that. For some reason I keep getting mind blocks when writing programs.

How do I solve this problem?

Please don't tell me to "write a program that will give Intrade suggestions on how to solve his problem."

Thanks

-Intrade

Intrade 33 Junior Poster

"Yes."

Intrade 33 Junior Poster

http://en.wikipedia.org/wiki/Memory_hierarchy

Is this accurate?

For example, whenever I am playing WoW directly from my iPod I realize that the application load time and (sometimes) run-time is much slower than if I were playing directly from my HDD.

Maybe I'm misunderstanding something?

Then again since its an application it is (mainly) using RAM, although there are sometimes downloaders involved that stream information onto either my iPod or HDD (depending where the application is launched).

Thanks!

Intrade 33 Junior Poster

Your caret is going to the end of the document because you're removing and re-inserting the entire content. The caret is just staying at the position of the last insert. You can fix that by changing these two lines in your SyntaxDocument2.insertString() method

String str2 = super.getText( 0, super.getLength() );
        
        super.remove( 0, super.getLength() );

slightly to get only the "upstream" portion of the document plus the string you are entering

String str2 = super.getText( 0, [B]offs+str.length()[/B] );

        super.remove( 0, [B]str2.length()[/B] );

That way you are only operating on the portion up to and including your current insert position and the caret stays at that position.

There is probably an easier way to do that with a DocumentFilter that doesn't require removing and re-inserting all of that content, but I've never used styled documents much so I can't say exactly without playing around with it more. You might take a look at them and see if it could save you all that insert, remove, and re-insert overhead.

If I understand the logic correctly--

-Step 2: grab the characters from position 0 to the Caret's current position plus the length of the String that was inserted

-Step 3: remove the characters from the document up to the already inserted String

//... And then of course iterate through each character up to the length of the removed content and insert each character given the appropriate styling per character.

Result: Given that the Caret is positioned at the location of the last …

Intrade 33 Junior Poster

This may be a sloppy method of doing so, but you can send a reference to the JTextPane that you are using and manipulate the Caret after you are done sending characters to the Document.

import javax.swing.*;

/**
 * Main frame for the program.
 * @author Bill
 *
 */
public class MainFrame extends JFrame {
    private static final long serialVersionUID = -7853618434340029314L;
    JTextPane editor = null;
    SyntaxDocument2 sd = null;
    
    /**
     * Make a main frame.
     */
    public MainFrame() {
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        editor = new JTextPane();
        sd = new SyntaxDocument2( editor );
        this.setSize( 500, 500 );
        editor.setDocument( sd );
        this.add( editor );
        this.setVisible( true );
    }
    
    /**
     * entry point of the program
     * @param args command line arguments
     */
    public static void main(String[] args) {
        new MainFrame();
    }
}
/**
 * 
 */
import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;


/**
 * @author Bill
 *
 */
public class SyntaxDocument2 extends DefaultStyledDocument {
    private static final long serialVersionUID = 8802669064394912853L;
    SimpleAttributeSet norm = new SimpleAttributeSet();
    SimpleAttributeSet tag = new SimpleAttributeSet();
    SimpleAttributeSet string = new SimpleAttributeSet();
    private JTextPane jp = null; // A reference to a JTextPane
    
    /**
     * Sets some stuff up.
     */
    public SyntaxDocument2( JTextPane jp ) {
    	this.jp = jp;  // assigning the passed reference to the globally scoped one
        StyleConstants.setForeground( tag, Color.green );
        StyleConstants.setForeground( string, Color.red );
    }
    /**
     * @see javax.swing.text.AbstractDocument#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
     */
    @Override
    public void insertString( int offs, String str, AttributeSet a )
            throws BadLocationException …