Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Classes and Objects
Classes
Declaring Classes
Declaring Member Variables
Defining Methods
Providing Constructors for Your Classes
Passing Information to a Method or a Constructor
Objects
Creating Objects
Using Objects
More on Classes
Returning a Value from a Method
Using the this Keyword
Controlling Access to Members of a Class
Understanding Class Members
Initializing Fields
Summary of Creating and Using Classes and Objects
Questions and Exercises
Questions and Exercises
Nested Classes
Inner Class Example
Local Classes
Anonymous Classes
Lambda Expressions
Method References
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Questions and Exercises
Enum Types
Questions and Exercises
Trail: Learning the Java Language
Lesson: Classes and Objects
Home Page >Learning the Java Language >Classes and Objects
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Questions and Exercises: Classes

Questions

  1. Consider the following class:

    public class IdentifyMyParts {    public static int x = 7;     public int y = 3; }
    1. What are the class variables?

    2. What are the instance variables?

    3. What is the output from the following code:

      IdentifyMyParts a = new IdentifyMyParts();IdentifyMyParts b = new IdentifyMyParts();a.y = 5;b.y = 6;a.x = 1;b.x = 2;System.out.println("a.y = " + a.y);System.out.println("b.y = " + b.y);System.out.println("a.x = " + a.x);System.out.println("b.x = " + b.x);System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

Exercises

  1. Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Be sure to keep your solution as you will be asked to rewrite it inEnum Types.


    Hint: 

    You can use theassert statement to check your assignments. You write:

    assert (boolean expression to test);

    If the boolean expression is false, you will get an error message. For example,

    assert toString(ACE) == "Ace";

    should returntrue, so there will be no error message.

    If you use theassert statement, you must run your program with theea flag:

    java -ea YourProgram.class

  2. Write a class whose instances represent afull deck of cards. You should also keep this solution.

  3. 3. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards.

Check your answers.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Summary of Creating and Using Classes and Objects
Next page: Questions and Exercises: Objects

[8]ページ先頭

©2009-2025 Movatter.jp