Reference no: EM132256257
Question: Introducing Classes, Objects and Methods
Instructions: 1. Make a copy of assignment template. Go to File => Make a copy (or download as a Word file.)
2. Complete definitions and attach Snipping Photos where appropriate
3. Place your name in the Title of each Assignment
1. For Example: CIS 36A- Assignment 1 - Basic Concepts - Irfan O.
4. Use the book or do online research to find answers.
5. Write your answers using a different font color. Find your own unique color.
6. Write answers in your own words. DO NOT COPY & PASTE from anywhere.
7. Submission: When done, go to File -> Download as -> Microsoft Word and then upload the file to Canvas.
Task 1: Definitions & Concepts
Instructions: Briefly answer the questions below.
1. Keywords: To you best knowledge, describe below words:
1. class =>
2. method =>
3. return =>
4. void =>
5. this =>
6. object =>
7. constructor =>
8. new =>
2. What is the difference between a class and an object?
3. How is a class defined?
4. How must a method return if it returns a value?
5. What name does a constructor have?
6. If a method returns no value, what must its return type be?
Task 2: Understanding Programming
Instructions: Answer each question below. Try to understand and explain the code. Do not put an IDE code screenshot.
1. Exercise 15: Suppose you have a class MyClass with one instance variable x. What will be printed by the following code segment?
MyClass c1 = new MyClass();
c1.x = 3;
MyClass c2 = c1;
c2.x = 4;
System.out.println(c1.x);
Explain your answer.
2. Exercise 16: Suppose a class has an instance variable x and a method with a local variable x.
1. If x is used in a calculation in the body of the method, which x is being referred to?
2. Suppose you needed to add the local variable x to the instance variable x in the body of the method. How would you do so?
3. Exercise 17: The following method has a flaw (in fact, due to this flaw it will not compile). What is the flaw?
void displayAbsX(int x) {
if (x > 0) {
System.out.println(x);
return;
}
else {
System.out.println(-x);
return;
}
System.out.println("Done");
}
1. Exercise 18: Create a method max( ) that has two integer parameters x and y and returns the larger of x and y.
2. Exercise 19: Create a method max( ) that has three integer parameters x, y, and z, and it returns the largest of the three. Do it two ways: once using an if-else-if ladder and once using nested if Statements.
3. Exercise 21: Find all the errors (if any) in the following class declaration:
Class MyCla$ {
integer x = 3.0;
boolean b == false
// constructor
MyClass(boolean b) { b = b; }
int doIt() {}
int don'tDoIt () { return this; }
}
Task 3: Programming Exercises
Instructions: Use any IDE to write and execute below exercises from the book chapter 3. Attach Snipping photos of your source code and execution of the code in the console. Make sure to create separate files for each exercise.
Chapter Examples: Follow the lectures to do below programs with the instructor.
1. Day 1: MethodDemo
2. Day 2: The last version of your VehicleDemo class
3. Create a BankAccount class with one integer instance variable called balance. Create a constructor with no initial parameters. Create three methods: one for deposit, one for withdrawal, one for get balance.
Excercises: 13. Create a Die class with one integer instance variable called sideUp. Give it a constructor and a getSideUp( ) method that returns the value of sideUp and a void roll( ) method that changes sideUp to a random value from This refers to the b instance variable, not the parameter. sch2207X_ch04_p119-154.indd 151 22/12/11 5:05 PM 152 chapter 4 Introducing Classes, Objects, and Methods 1 to 6. (To see how to generate a random integer between 1 and 6, look at the last exercise in Chapter 2.) Then create a DieDemo class with a main method that creates two Die objects, rolls them, and prints the sum of the two sides up.
14. Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Give it a constructor with two parameters for initializing the two instance variables and give it a getSuit( ) method and a getRank( ) method that return the values of the two instance variables. Then create a CardTester class with a main method that creates five Cards that make up a full house (that is, three of the cards have the same rank and the other two cards have the same rank) and prints out the ranks and suits of the five Cards using the getSuit( ) and getRank( ) methods.
22. Create a Swapper class with two integer instance variables x and y and a constructor with two parameters that initialize the two variables. Also include three methods: A getX( ) method that returns x, a getY( ) method that returns y, and a void swap( ) method that swaps the values of x and y. Then create a SwapperDemo class that tests all the methods.
24. Create a USMoney class with two integer instance variables dollars and cents. Add a constructor with two parameters for initializing a USMoney object. The constructor should check that the cents value is between 0 and 99 and, if not, transfer some of the cents to the dollars variable to make it between 0 and 99. Add a plus method to the class that takes a USMoney object as its parameter. It creates and returns a new USMoney object representing the sum of the object whose plus( ) method is being invoked and the parameter. It does not modify the values of the two existing objects. It should also ensure that the value of the cents instance variable of the new object is between 0 and 99. For example, if x is a USMoney object with 5 dollars and 80 cents, and if y is a USMoney object with 1 dollar and 90 cents, then x.plus(y) will return a new USMoney object with 7 dollars and 70 cents. Also, create a USMoneyDemo class that tests the USMoney class.
25. Create a Date class with three integer instance variables named day, month, year. It has a constructor with three parameters for initializing the instance variables, and it has one method named daysSinceJan1( ). It computes and returns the number of days since January 1 of the same year, including January 1 and the day in the Date object. For example, if day is a Date object with day = 1, month = 3, and year = 2000, then the call sch2207X_ch04_p119-154.indd 153 22/12/11 5:05 PM 154 chapter 4 Introducing Classes, Objects, and Methods date.daysSinceJan1( ) should return 61 since there are 61 days between the dates of January 1, 2000, and March 1, 2000, including January 1 and March 1. Include a DateDemo class that tests the Date class. Don't forget leap years.
26. What is the difference, if anything, between the following two implementations of the method doIt( )?
void doIt(int x) {
if(x > 0)
System.out.println("Pos");
else
System.out.println("Neg");
}
void doIt(int x) {
if(x > 0) {
System.out.println("Pos");
return;
}
System.out.println("Neg");
}
Textbook: Java Programming - A Comprehensive Introduction (By Herbert Schildt and Dale Skrien)