Question 1)
Write a class StringLength such that
java StringLength string1 string2 .. stringN returns
the sum of length of all the strings..
Example, java StringLenght How long are the strings
gives me 3 + 4 + 3 + 3+ 7 = 20.
Question 2)
Write a class RemoveDigit such that
java RemoveDigit string1 string2 ... stringN remove
all the digits from all the strings and display them.
Example,
C:>java RemoveDigit PIC10A is number1
PICA is number
Useful methods:
class Character
static boolean isDigit(char ch) : return true if
ch is a digit.
class String
char[] toCharArray(): return the string in form of an array of characters.
Question 3)
We have
String string = "hi";
char[] array = {'h','i'};
Which of the following lines are correct?
(a) string.length;
(b) string.length();
(c) array.length;
(d) array.lengt()
Question 4)
(a) Write down some constructors for JLabel,.
(b) Write down some constructors for Color and Font
(c) do you know how to use the following methods for
Graphics class? drawString, drawOval, fillOval, drawRect,
fillRect, drawArc, fillArc, setColor, setFont?
Question 5) Do you remember the rules for identifies? Which of the followings
are legal identifies? (i) aBook (ii) 2Books (iii) a_long_name (iv) go2where (v)
funny-book (vi) one+two (vii) public
Question 6)
I have 2 classes in 2 different files.
What is the output after we run java Test?
public class Test {
public static void main(String[] args) {
Holder h = new Holder();
h.held = 100;
h.bump(h);
System.out.println(h.held);
}
}
public class Holder {
public int held = 0;
public void bump(Holder theHolder) {
theHolder.held++;
}
}
Question 7)
- Explain 2 usages of
this
- Explain 2 usages of
super
- Explain 3 usages of
final
Question 8)
Write a class ShowShape extends JFrame. Such that
java ShowShape circle
displays a circle with upper left corner at (50,50) and diameter 100
with red color.
java ShowShape square
displays a square with upper left corner at (50,50) and length 100
with yellow color.
And java ShowShape other strings displays nothing.
Here is part of ths code :
// import statements
public class ShowShape extends JFrame {
private String shape;
// your constructor
// paint method
// main method
public static void main(String[] args) {
ShowShape s;
// your code
s.setSize(200,200);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
}
}
Question 9
Create a class called Match.
Match string0 string1 string2... stringN
displays strings from string1 to... stringN that
contains at least one character from string0.
The program is case sensitive.
Here is output
C:> java Match o apple orange banana
orange
C:> java Match op apple orange banana
apple orange
C:> java Match rstu Russia China Japan France England Korea Canada
Russia France Korea
C:> java Match xyz Russia China Japan France England Korea Canada
<--------------nothing shows out
C:> java Match
<--------------nothing shows out
Here are some useful methods :
class String
public int indexOf(int ch):
Returns the index within this string of the first occurrence of the specified character.
If a character with value ch occurs in the character sequence represented by this String object,
then the index of the first such occurrence is returned.
If no such character occurs in this string, then -1 is returned.
public char[] toCharArray():
Converts this string to a new character array.
public char charAt(int index)
Returns the character at the specified index.