Question

In: Computer Science

Intro To Programming In Java(2nd Edition) I'm currently trying to study and understand stuff better, but...

Intro To Programming In Java(2nd Edition)

I'm currently trying to study and understand stuff better, but I need some help.

Can someone please help me define/understand the following:

Section 2.1

-Static Methods
-Control Flow
- Input Parameters and Arguments (single and multiple)
-Overloading
-Return Statement
-Side Effects
-Scope and Lifetime
-Pass-by-Value (Primitives) vs Pass-by-Reference (Arrays)
-Reason of Use: Organize Code

Section 3.1

-Strings (substring, charAt, split, indexOf, contains, length, and == vs equals)
-Constructor
-Instance Methods
-Reference types(String and Arrays) vs Primitive Types(int, boolean, char, double)
-Difference between Static and Instance methods.

Section 3.2

-API and UML
-Class
-Access Modifiers (private, public, and final)
-Instance Variables
-Instance Methods
-Scope and Lifetime
-Constructors

Solutions

Expert Solution

To understand any of these first you need thoroughly what is a class in Java, what is an instance of a class? When do you want to create an instance of a class and when do you not want to create an instance of a class?

A class is a just template or a skeleton that defines a combination of data objects and methods used in the class. The methods in the class are used to perform different operations on the different data objects defined in this class.

Now once we define the class how can we use it?

Class is just a definition. We can use class by creating objects.

So now what is an object?

Object is an instance of the class.

What does instance of a class mean?

Class is just a definition. At compile time, class definition is validated to see if the data objects use valid data types and methods use these data objects etc. Only when an instance of a class is created that's when memory is allocated for this instance called object. New memory is allocated for every object that is created for this class.

Now that we understand instance of a class. Let us try to understand how can we create an instance of the class?

This is where constructors come into picture. Instance of a class can be created only by using constructors.

What are constructors?

Constrcutors are used to create instance of an object. While creating an instance of the class we can also initialize some values to the different variables used in the class. The name of the constrcutor is the same as the name of the class and it needs to be public by default.

How can we initialize these values using constructors?

We can initialize these values using constructors by passing these values as arguments or as parameters to the constructor.

We can create multiple constructors with different arguments so that we can use appropriate one to create an instance of the class. This is also called Overloading. Method overloading is nothing but having different methods with the same name but different types or different number of parameters.

There are cases when some of the data and methods defined in the class can be used by classes or variables or methods outside of this class. Such type of methods are called utility methods. These methods will be defined as static. These static methods/variables are global to the class in which they are defined and can be accessed without creating instances for the class. Static variables or methods are not tied to an instance of a class. These are global to the class. So different instances of the class (different objects) can share and access these static variables and methods.

what is data type?

For any data defined as a variable we can declare the type for this variable. Declaring the type for the variable helps in assigning memory for that variable. For example when we declare a variable to be of type byte 8 bits of memory will be assigned to this variable during compile time. Data type can be primitive or non-primitive. Primitive data types are pre-defined, have fixed size and are common to most languages. Byte, short, int, long, char, float, double, boolean are primitive data types. In Java, byte needs 8 bits of memory, int needs 32 bits of memory etc. You can refer to this in your text book for understanding it better.

Non-primitive data types are the data that each object created for a class holds. This differs from one class definition to another class definition.

Now let us understand what is a method?

Method is a routine that usually performs one single task. Given the input, method processes that input and gets the output. This output can be returned back to the caller. A caller is also piece of code (usually in another method) that calls this method. Main use case of the method is it's reusability. That is for different inputs we can use the same method and get desired outputs.

For example, let us say we need to find the sum of two elements. We can get this done using '+' operator between two elements defined as variables in java. First let us understand what is a variable. Variable is a holder to

How is a method defined?

Method definition is called signature of the method. Method signature has method name, input (one or more arguments) and a return value. This return value is sent back to the caller which can use this value for doing further computations in it's own code.

What is call by value?

Call by value is when we pass the value of any variable as parameter to another method.

what is call by reference?

Call by reference us when the address of the variable is passed as a parameter to another method.

what will happen when we pass arguments to a method using call by value?

When we pass arguments to a method using call by value these values are used as input to the method and processing is done on the input using these values and the output is returned. The actual variables that were passed as arguments are used as read-only and they are not disturbed.

For example: Let us say we have three methods. First method is getResultUsingOperator and other two methods are sum and multiply.

Method signature for getResultUsingOperator: int getResultUsingOperator(int number1, int number2, char operator);

Method signature for sum: int sum(int num1, int num2);

Method signature for multiply: int multiply(int num1, int num2);

Code for getResultUsingOperator and sum and multiply are as listed below:

The values of the number1 and number2 in the method getResultUsingOperator will not change after calling sum or multiply methods even though their values are changed in sum and multiply methods because these arguments are passed using call by value. You will understand this when I explain the scope of a variable.

Reference types(String and Arrays) vs Primitive Types(int, boolean, char, double)?

As explained above primitive data types are pre-defined and have a fixed size. Reference types are for non-primitive types. A reference is to the variable/object is stored in memory instead of the value of the variable. For example, if we assign one variable to another variable.

For primitive type:

int a = 10;

int b = a;

In this case, a new variable b is created. 4 new bytes of memory is assigned to b and only the value of the variable a is copied into the variable b.

For Reference type like Array would look like this:

That is array 'b' is a reference to array 'a'. If we change value in Array 'a' it will be changed in Array 'b' also as both 'a' and 'b' are pointing to the same array.

In java always the variables are passed using call by value. For reference types the reference is called by value. So any value change for reference types passed as arguments will change the value of this reference type.

what is scope and lifetime?

Scope of a variable is the areas where all can the variable be used. Lifetime of a variable is the region in which the variable is active. That is region in which memory is still allocated for this variable. Usually the scope for a local variable is within the code block (code between '{' and '}') in which it is defined. Scope for a static variable is within the class in which it is defined.

Coming to access modifiers: Access modifiers like public, protected, private and default are used to define the scope for a variable or method.

UML: This is a pictorial representation of the class design of your program. UML can be used to draw a high-level represention for your application. This is useful to design your application before startiing coding. The dependencies between different classes within your application can also be defined using UML.

API: This is used to wrap all classes and packages together.

I gave you the concepts in general. There is lot of work from your side to learn these in-depth. I would suggest you to dig deep and try to understand the basics in general to understand all these concepts better.

Hope this helps to understand the text book material. Please let me know in case you did not understand something or you need more information on anything.


Related Solutions

I'm in a intro to programming class using C++. In some examples in my book, they...
I'm in a intro to programming class using C++. In some examples in my book, they use the letter ' i ' to do certain things like a 'for loop'. I was looking around the internet and I can't seem to find what that specific letter does. In all the websites I went to about operators it wasn't listed. Do I need to define it? Or just use it like it is? Example: for(int i = 0; i < 4;...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: TV. Define Class TV Write the class header Class Variables Class TV is to have the following instance variables: 1.channel of type int 2. volumeLevel of type int 3. isOn of type boolean Each data member is to have an access specifier of private. Constructor Class TV is to have a single, no-arg constructor that initializes the...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
JAVA, trying to better learn and use JAVA //Please note throughout if possible looking to better...
JAVA, trying to better learn and use JAVA //Please note throughout if possible looking to better understand the process. Write the following class: Box Define Class Box Write the class header Class Variables Class Box is to have the following private data members: height of type double width of type double length of type double Constructors Class Box is to have two constructors with the following specifications: a no-arg constructor that initializes each double data member to zero a constructor...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
Please show how the solution was arrived at I'm trying to understand how to do the...
Please show how the solution was arrived at I'm trying to understand how to do the problem. 4.(2) The fern is defined as the unit of force required to accelerate a unit of mass, called the bung, with the gravitational acceleration on the surface of the moon, which is one-sixth the normal gravitational acceleration on earth. a) What is the conversion factor that would be used to convert a force from the natural unit to the derived unit in this...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently. Problem 5: Player Move Dungeon (10 points) (Game Development) You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT