How must a method return if it returns a value

Assignment Help JAVA Programming
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)

Reference no: EM132256257

Questions Cloud

Why did you select phd in information technology : Why did you select PhD in Information Technology? Why did you select University of the Cumberlands? As an individual, what are your strengths and weaknesses.
Total amount of income and expenses for the month : This interactive program focuses on if/else statements, Scanner, and returning values. a file named Budgeter.java. To use a Scanner for console input
Benefits of holistic approach in change management : Benefits of holistic approach in change management
Estimate average length of firm short-term operation cycle : Estimate the average length of the firm's short-term operating cycle. How often would the cycle turn over in a year
How must a method return if it returns a value : CIS 36A What is the difference between a class and an object? How must a method return if it returns a value? What name does a constructor have?
Common objective function for project selection problems : A common objective function for project selection problems is to. In a project selection problem formulation:
Explain the relevance of the critical assignment to the uso : Explain the relevance of the Critical Assignment to the USO by first identifying and repeating language from the USO being documented.
More ways to measure the level of customer satisfaction : Discuss two or more ways to measure the level of customer satisfaction. How would you implement these measurement tools as a manager and why?
Discuss about the dominant view of educational leadership : Do you agree with him that the "dominant view of educational leadership has served to limit teacher involvement in the development and planning of curricula?"

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd