Explore BrainMass
Share

simple java questions

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Please explain these:

1. In general are attributes public and methods are private?

2. Suppose you must define a class called PlayingCard. What are two examples of attributes and two examples of behaviors for this class?

3. Define a single constructor for this class.
public class Student
{
//attributes
private String studentID;
private double gpa;
private boolean hasFunding;
private String major;

//constructor definition

}

4. Rewrite the above code so that it requires as few lines as possible:
String s;
Fraction f;
f = new Fraction( 100, 200 );
s = f.toString( );
System.out.println( s );

5. How many objects are instantiated in the code below:
String s;
Fraction f, g;
Point w, x;
JFrame j;
f = new Fraction(25, 35);
g = f;
w = new Point(5, 10);

6. How many objects are instantiated in the code below:
String title = "Push Me"
JButton one = new JButton(title);
JButton two;
two = one;

7. Is length is an attribute of the String class?

8. Does this line of code compile:
String herName = new String("Naomi");

9. Is an object instantiated in the heap when this line of code is executed?
String hisName = "Dan Jones";

10. In the following code what is displayed on the screen (case sensitive)
String x;
x = new String("Goodbye");
x.toUpperCase( );
System.out.println( x );

11. In the following code how many String objects are instantiated?
String y, z;
y = new String("This is easier than I thought!");
z = y.substring(11, 13);.

© BrainMass Inc. brainmass.com October 25, 2018, 9:38 am ad1c9bdddf
https://brainmass.com/computer-science/java/simple-java-questions-581022

Solution Preview

See the attached file.

1. In general are attributes public and methods are private?
In general attributes are private and methods are public
2. Suppose you must define a class called PlayingCard. What are two examples of attributes and two examples of behaviors for this class?
Attributes: cardSuite, cardValue
Methods: getSuite, getValue
3. Define a single constructor for this class.
public class Student
{
//attributes
private String ...

Solution Summary

The solution discusses the answers to some simple java questions.

$2.19
See Also This Related BrainMass Solution

Question about Java program to memorize a sequence of colors

The files must be called <YourNameProg7.java>.
Ensure you include ALL files required to make your program compile and run.
I would like to see your .java files only.
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Basic Requirements
Write a program that tests the user's ability to memorize a sequence of colors. As shown in the sample session, the program starts off by displaying a dialog box with a list of colors that are to be memorized - red, white, yellow, green, and blue. The user then enters the colors one at a time in a text box. If the user makes a mistake, the program prints a "Sorry" message. If the user correctly enters all the colors, the program prints a "Congratulations" message. Note that when the sorry or congratulations message is printed, the window's original components get cleared away.
As always, you are required to write elegant code. In particular, you should avoid hard coding the color values in the interior of your program. You should declare those values one time in an array at the top of the program.
Note:
? Your program should contain a class named YourNameProg7.
? Use a simple FlowLayout layout manager scheme.
? Use an inner class for the listener.

As always:
? Limit your use of class variables and instance variables - use them only if appropriate.
? Use appropriate modifiers for your methods. The modifiers we've discussed are private, public, static, and final.
? Use helping methods if appropriate.
? Mimic the sample session precisely. In particular, note the dialog box's text, the window's title, and the window's text.

First sample session:
The opening dialog box:

After closing the dialog box, here's the main window:

After typing the first color:

After pressing enter and then typing the second color:

After entering all five colors correctly:

Second sample session:
After closing the dialog box and typing the first color:

After pressing enter and then typing the second color:

After pressing enter:

You can get full credit for completing this section only.
Extra Credit
Provide a hint button that causes the current color's first letter to appear in the text box. For example, since the first color is red, the first hint should be r. The hint button must cause focus to be put on the text box (i.e., the cursor should appear within the text box without the user having to click there with the mouse). To cause focus to be put on a component, use the requestFocusInWindow method. See Sun's API documentation for requestFocusInWindow details.
Sample session:
After closing the dialog box, here's the main window:

After clicking the hint button:

After typing the first color:

After pressing enter and clicking the hint button:

After entering all five colors correctly:

View Full Posting Details