Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Straight Line programming

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Straight Line programming

    I have a statement like this

    Stm s = new CompoundStm(new AssignStm("a", new NumExp(6)), new PrintStm(new PairExpList(new NumExp(8), new LastExpList(new IdExp("b")))));

    and I have to get the output like this :

    System.out.println("The Total Of Statement is : ");
    System.out.println("The Max of Print Argument is : ");
    System.out.println("The deep of tree is : ");
    System.out.println(" That Code is : ");

    I was made a several class like this :

    public abstract class Stm {}
    public class AssignStm extends Stm {
    Exp id; String ids;
     
        public AssignStm(String ids,Exp id ){
           this.ids = ids;
           this.id = id;
    }}
     
    public class CompoundStm extends Stm {
    Stm stm1; Stm stm2;
     
        public CompoundStm(Stm stm1,Stm stm2){
           this.stm1 = stm1;
           this.stm2 = stm2;
    }}
     
    public class PrintStm extends Stm {
    LastExpList last; 
    PairExpList pair;
     
        public PrintStm(LastExpList last){
        this.last = last;
        }
     
        public PrintStm(PairExpList pair){
        this.pair = pair;
    }
     
    public abstract class Exp {}
    public class EseqExp extends Exp {
        Exp exp;
        Stm stm;
     
        public EseqExp(Stm stm, Exp exp){
            this.stm = stm;
            this.exp = exp;
     
        }
     
    }
     
    public class IdExp extends Exp {
        String id;
     
        public IdExp(String id){
            this.id= id;
        }   
    }
     
    public class NumExp extends Exp {
        int num;
     
        public NumExp(int num){
            this.num= num;
        }    
    }
     
    public class OpExp extends Exp {
        IdExp id; IdExp id1; NumExp num; NumExp num1;
     
        public final static String plus="+";
        public final static String minus="-";
        public final static String kali="*";
        public final static String bagi="/";
     
        public OpExp(NumExp num, String a ,IdExp id1  ){
            this.num= num;
            this.id= id1;
     
        }
     
        public OpExp(IdExp id, String a ,NumExp num  ){
            this.id= id;
            this.num= num;
     
        } 
     
        public OpExp(NumExp num, String a ,NumExp num1  ){
            this.num= num;
            this.num1= num1;
        } 
     
        public OpExp(IdExp id,  String a, IdExp id1 ){
            this.id= id;
            this.id1= id1;
        }    
    }
     
    public abstract class ExpListParent {
     
    }
     
    public class LastExpList extends ExpListParent{
        Exp exp;
     
        public LastExpList(Exp exp){
            this.exp = exp;
        }
    }
     
    public class PairExpList extends ExpListParent{
        Exp exp; LastExpList last;
     
        public PairExpList(Exp exp, LastExpList last){
            this.exp = exp;
            this.last = last;
        }
    }

    Can anyone get help me to solve this ?
    What should I do ?
    Thanks a tons before.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,323
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: Straight Line programming

    What's your question? What does this code do? How does the code's execution differ from what you expect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Straight Line programming

    from the new stm, I can this :

    The Total Of Statement is :
    The Max of Print Argument is :
    The deep of tree is :
    That Code is :

    So, if I have statement like this :

    Stm s = new CompoundStm(new AssignStm("a", new NumExp(6)),
    new PrintStm(new PairExpList(new NumExp(8), new LastExpList(new IdExp("b")))));

    The Total Of Statement is : 2
    The Max of Print Argument is : 3
    The deep of tree is : 5
    That Code is : a =6; print(8,b)

    I am totally stress...

    best regard from Indonesia. Sorry for my bad english

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Straight Line programming

    Quote Originally Posted by Fadly Massere View Post
    So, if I have statement like this :

    Stm s = new CompoundStm(new AssignStm("a", new NumExp(6)),
    new PrintStm(new PairExpList(new NumExp(8), new LastExpList(new IdExp("b")))));

    The Total Of Statement is : 2
    The Max of Print Argument is : 3
    The deep of tree is : 5
    That Code is : a =6; print(8,b)
    Reading the code, I can have a good/valid idea of what that code should "express". In other words, I understand what you would like to do in general.
    But the fact is that you must have a clear vision of where these results (2, 3, etc...) should come from!

    Take for example the first result "The Total Of Statement is : 2". Why 2? Try to think ....
    You have an Stm variable that references a CompoundStm object. Note here, the real object is a CompoundStm but you "see" it only as the more general Stm base type.
    Speaking with words, you should say "hey Stm object, give me the number of statements!". The Stm class should declare an abstract method that must be implemented by all Stm concrete subclasses. The implementation in CompoundStm "knows" that, being a compound statement, it has 2 statements.
    But note that in CompoundStm the two objects are of type Stm, so theoretically it's possible that a CompoundStm contains in turn other CompoundStm.
    So the same request should go to both stm1 and stm2 until you find a "final" statement that doesn't contain other statements.

    To be more clear, suppose a method getStatementsCount() in Stm:
    - you invoke getStatementsCount() on s (Stm)
    - the concrete implementation (CompoundStm) invokes getStatementsCount() on stm1 and then on stm2
    - stm1, being a AssignStm, is not a container for other statements, and should return 1 for getStatementsCount()
    - stm2, being a PrintStm, is not a container for other statements, and should return 1 for getStatementsCount()
    - CompoundStm get 1 and 1, and so should return 2.

    I hope to have explained (for my english ) the concept at high level. Try to think about what I said.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. The Following User Says Thank You to andbin For This Useful Post:

    Fadly Massere (January 8th, 2014)

  6. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Straight Line programming

    Quote Originally Posted by andbin View Post
    Reading the code, I can have a good/valid idea of what that code should "express". In other words, I understand what you would like to do in general.
    But the fact is that you must have a clear vision of where these results (2, 3, etc...) should come from!

    Take for example the first result "The Total Of Statement is : 2". Why 2? Try to think ....
    You have an Stm variable that references a CompoundStm object. Note here, the real object is a CompoundStm but you "see" it only as the more general Stm base type.
    Speaking with words, you should say "hey Stm object, give me the number of statements!". The Stm class should declare an abstract method that must be implemented by all Stm concrete subclasses. The implementation in CompoundStm "knows" that, being a compound statement, it has 2 statements.
    But note that in CompoundStm the two objects are of type Stm, so theoretically it's possible that a CompoundStm contains in turn other CompoundStm.
    So the same request should go to both stm1 and stm2 until you find a "final" statement that doesn't contain other statements.

    To be more clear, suppose a method getStatementsCount() in Stm:
    - you invoke getStatementsCount() on s (Stm)
    - the concrete implementation (CompoundStm) invokes getStatementsCount() on stm1 and then on stm2
    - stm1, being a AssignStm, is not a container for other statements, and should return 1 for getStatementsCount()
    - stm2, being a PrintStm, is not a container for other statements, and should return 1 for getStatementsCount()
    - CompoundStm get 1 and 1, and so should return 2.

    I hope to have explained (for my english ) the concept at high level. Try to think about what I said.
    Finally, thanks before.
    Literally, I,m stiil Confused. But thank you very much.
    should I make getStatementsCount() method for each class, and then I compare ?
    or just one method in main class ?

    what about instanceOf ? can I use this ?

  7. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Straight Line programming

    Quote Originally Posted by Fadly Massere View Post
    should I make getStatementsCount() method for each class, and then I compare ?
    or just one method in main class ?

    what about instanceOf ? can I use this ?
    No, now I re-explain, I hope better.

    First, I said getStatementsCount just for example, but the method name is absolutely up to you.

    getStatementsCount() should be an abstract method in Stm. Indeed, Stm is itself abstract because it represents an abstract concept: "a statement", without any particular behaviour. Conceptually Stm is like classes as Animal, Figure, Vehicle, that are generic and from which you can derive classes eg. Cat/Dog, Circle/Square, Car/Truck etc... that are more "concrete".

    Then you have several concrete subclasses of Stm. In each of these classes you must do an override of getStatementsCount(). Technically keep the same signature (method name and parameter types) and return type.
    But here you must do something more concrete, you have to return a value, the count of the statements. How? For each class think about what the object represents and contains.

    And try to think a similar logic for the other informations.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. First line of Java Programming- package help
    By iNoshift in forum Java Theory & Questions
    Replies: 4
    Last Post: June 3rd, 2013, 09:38 PM
  2. Getting my paddle to move left and right in a straight line-STUCK
    By warnexus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2011, 08:06 PM
  3. Straight and Straight Flush?!?!
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 09:44 PM
  4. Serail Port Programming regarding phone line hook
    By maskey_dipesh in forum Java SE APIs
    Replies: 2
    Last Post: September 4th, 2009, 05:12 AM

Tags for this Thread