Lecture 16 - Solutions

  1. DoubleField D is displaying a real number. We wish to have D display a number that is twice the magnitude of its current real number.
    Example: If D displays 3.6, then the new value would be 7.2.

    double d1;
    
    d1 = D.getDouble();
    d1 = d1 * 2;
    D.setDouble(d1);
    
  2. StringField A contains a string and StringField B contains another one. We wish to display in StringField C a string which is the join or concatenation of the other two. Thus if A contains "QWER" and B contains "CVBNM", then C should receive and display "QWERCVBNM".

    
    String s1, s2, s3;
    
    s1 = A.getString();
    s2 = B.getString();
    s3 = s1 + s2;
    C.setString(s3);
    
  3. IntField's A and B contain integers. We wish to have IntField C display the largest of the two. Thus if A and B hold 7 and 23, then C will display 23.

    int i1, i2;
    
    i1 = A.getInt();
    i2 = B.getInt();
    if (i1>i2) 
    {
      C.setInt(i1);
    } 
    else
    {
      C.setInt(i2);
    }
    
  4. StringField's A and B hold strings. We wish to interchange them. If A and B hold "Durham" and "Raleigh", respectively, after the exchange, they will hold "Raleigh" and "Durham", respectively.

    String s1, s2;
    s1 = A.getString();
    s2 = B.getString();
    A.setString(s2);
    B.setString(s1); 
    
  5. Suppose StringField A holds a string and we wish to create a new string with the last three characters removed and display it in A. Thus if A holds "Durham", it's new value should be "Dur".
    (Note: You should be able to create a long list of similar problems and solve them.)

    String s1;
    
    s1 = A.getString();
    s1 = s1.substring(0,s1.length()-3);
    A.setString(s1);
    
  6. StringField R is displaying a string. We wish to display in IntField D an integer that tells where the first example of an "A" is in R.
    Example: If R has "CGFDSAPOIU", then D will display the integer 5.

    String s1;
    int i1;
    
    s1 = R.getString();
    i1 = s1.indexof("A");
    D.setInt(i1);
    
  7. Arrays: How do you declare one?
    double value[];
    
    How do you construct one?
    value = new double[300];
    
    How do you put a 6 into every entry of an integer (int) array. Assume that numElts contains the number of elements in the array.
    int value[];
    value = new int[100];
    
    i = 0;
    while(i<numElts)
    {
      value[i] = 6;
      i = i+1;
    }
    
    How do you put a "Yes" into every entry of a String array? Assume that last contains the final index of the array that you want to set.
    String value[];
    value = new String[20];
    
    i = 0;
    while(i<=last)
    {
      value[i] = "Yes";
      i = i+1;
    }
    
  8. How do you add up the integers in an integer array? How do you find the largest integer in an integer array?

    See the 20 example problems and solutions from a previous handout.

Action Routine Problems

  1. We wish to push a Button called initializeA that will remove the contents of StringField A and replace it with nothing (an empty String).

    Say there is a String s1:

    public boolean action(Event e, Object obj)
    {
      if (e.target==initializeA) {
      {    
        s1 = A.getString();
        A.setString("");
        return true;
      }
      return true;
    }
    
  2. We wish to push a Button called EX that will exchange the first and last characters in the string in StringField TR.

    Say we have String trfull, trfirst, trlast, trmiddle;:

    public boolean action(Event e, Object obj)
    {
      if (e.target==EX) {
      {    
        trfull = TR.getString();
        trfirst = trfull.substring(0,1);
        trmiddle = trfull.substring(1,trfull.length()-1);
        trlast = trfull.substring(trfull.length()-1,trfull.length());
        trfull = trlast + trmiddle + trfirst;
        TR.setString(trfull);
        return true;
      }
      return true;
    }
    
  3. We wish to push a Button called plus that will put the sum of the real numbers in double array R, which has 10 entries, into DoubleField SUM.

    Assume we have a double rsum and an int i

    public boolean action(Event e, Object obj)
    {
      if (e.target==plus) {
      {    
        rsum = 0.0;
        i = 0; 
        while(i<=9) 
        {
          rsum = rsum + R[i];
          i = i+1;
        }
        SUM.setDouble(rsum);
        return true;
      }
      return true;
    }
    
  4. We wish to push a Button called del that will remove a character from StringField A. The character that will be removed will be given by the integer in the IntField B. We assume the characters are numbered 0, 1, 2, etc. Thus if A contains "ABCDEFGHI" and B holds 4, then A will now display "ABCDFGHI".

    Assume we have a String afull, afront, aback and int index

    public boolean action(Event e, Object obj)
    {
      if (e.target==del) {
      {    
        index = B.getInt();
        afull = A.getString();
        afront = afull.substring(0,pos);
        aback = afull.substring(pos+1,afull.length());
        afull = afront+aback;
        A.setString(afull);
        return true;
      }
      return true;
    }
    

Class definitions and objects

You may be given the definition of Book from the class notes and asked questions: How do you create a method called strInTitle that receives a string from the outside and determines whether that string appears anywhere in the Book title. The method should return a String: "Yes" if it does and "No" otherwise.
Here is an example usage: s1 = a1.strInTitle("Mountain");
This will place into s1 the string "Yes" if "Mountain" appears anywhere in the title of the Book a1.

public String strInTitle(String test)
{
  if (title.indexof(test) == -1) 
  {
    return "No";
  } 
  else 
  {
    return "Yes";
  }
}


See more examples on the September 28 and October 2 handouts.