Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Nature of Things The Java platform (Java language plus Java API) includes ma

ID: 3888693 • Letter: T

Question

The Nature of Things
The Java platform (Java language plus Java API) includes many useful constants and utility methods in wrapper classes related to the primitive type they represent. For example, the Integer class contains the constant Integer.MAX_VALUE and Integer.MIN_VALUE. You can use these constant to find out if the value of a number is in the range of the integer data type. The Character class, contains the method Character.isDigit() among others. The Float class contains three very useful constants – NaN (Not a Number), POSITIVE_ INFINITY and NEGATIVE_ INFINITY. You will find the same constant in the Double class. They are very useful when you what to check if the CST8132-OOP, HA02, MMXIs Page 2 of 3 result from some arithmetic operation is correct. For example, 1.0 / 0.0 will produce POSITIVE_INFINITY; square root of -1 will produce NaN. Note: You cannot compare these constants to some primitive type value. You must use an appropriate method, for example isNAN().

An important fact is that wrapper classes including String are immutable, which means they cannot be changed. They do not provide methods (called mutators or setters ) that allow the programmer to change their values. If you want to have an integer object containing a different value, you must create a new Integer object. Reference: Textbook – Chapter 16, Sections 16.3 “Type-Wrapper Classes”, and 16.4 “Autoboxing and Auto-Unboxing”.

Syntax, Semantic Dissection, Code Example, and Program Code Dissection
Wrapping a primitive type in an object:

Before Java 5:
int a = 5; // declare and initialize a primitive int
Integer oa = new Integer(a); //wrap a in Integer object
Integer oa = new Integer(5); //wrap 5 in Integer object

After Java 5:
int a = 5;
Integer oa = a; //wrap a in Integer object
Integer oa = 5; //wrap 5 in Integer object


This new language feature is known as “autoboxing.” It is convenient, saves some typing, but actually, the compiler will convert your code to the one shown under Before Java 5. The autoboxing can be used only for primitives and wrappers of the same types i.e. Integer and int, Float and float and so on. In all other cases you must use a constructor or an appropriate method.

Unwrapping a primitive type from an object:
Before Java 5:
int a; // declare a primitive int
Integer oa = new Integer(5); //wrap 5 in Integer object
a = oa.intValue();//unwrap the Integer object

After Java 5:
int a; // declare a primitive int
Integer oa = 5; //wrap 5 in Integer object
a = oa; //unwrap the Integer object

This new language feature is known as “auto-unboxing.” It is convenient, saves some typing, but actually, the compiler will convert your code to the one shown under Before Java 5. The auto-unboxing can be used only for primitives and wrappers of the same types i.e. Integer and int, Float and float and so on. In all other cases you must use a constructor or an appropriate method. CST8132-OOP, HA02, MMXIs Page 3 of 3 The new auto boxing/unboxing feature allows the programmer to write expression involving primitive and object types. For example: a = oa + 5; will compile and work. Mixing primitive and wrapper types in expressions is not considered a good programming practice.

Hybrid Activity Exercise
Exercise 1
Use your browser to open the Java API documentation. Find the java.lang package and explore carefully wrapper classes for primitive types you have used (skip Void).

Exercise 2
Your task is to write a simple Java class (program) called Wrapper. Write a method with the following signature and return value: public Long addIntegers(Integer a, Integer b) {…your code here…} The method must return the result from the addition of a and b. Try Before Java 5 and After Java 5 syntax.

Write another method:
public Double divideFloats(Float a, Float b) {…your code here…}
Write a main method and test your addInteger() and divideFloats() methods. See what will happen when you divide a by b and b=0. The method must return null if the result from the division is infinity or NaN.

Questions
Q1. Do all primitive data types have a corresponding wrapper class?
Q2. Will the following line compile?
Float f = 5.0;

Submission
Using the submission link, copy and paste your source code for your divideFloats() method. To earn the marks you must submit your source code by the end of Week 5. The exercise will be marked according to the following marking method: public int markHA2E2(Boolean submitted, Boolean correct){ int mark = 0; if(submitted & correct.equals(true)) mark = 2; return mark; }

Explanation / Answer

Given below are the answers for the question. Hope it helps. If it did , please do rate the answer. Thank you very much.


Q1)
The primitive types in Java are - byte, short, int, long, float , double, char, boolean. Looking at the Java API page,
the correspoding wrapper classes are Byte, Short, Int, Long, Float , Double, Character, Boolean. So all primitive types have wrapper classes. Please not for char type , the wrapper class is Character.
Q2)
Yes the line Float f = 5.0; will compile, because of boxing the float value 5.0 is converted into the object class Float

Java Program


public class Wrapper {
public Long addIntegers(Integer a , Integer b)
{
long result = a + b;
return result; //result gets Wrapped into an ojbect of Long
}
public Double divideFloats(Float a, Float b)
{
double result = a / b;
if(Double.isInfinite(result) || Double.isNaN(result))
return null;
else
return result; //the result gets wrapped into an object of Double
}
public static void main(String[] args) {
Wrapper w = new Wrapper();
int a = 4 , b = 5;
System.out.println("a + b = " + w.addIntegers(a, b));
float a1 = 15, b1 = 4;
System.out.println("a1 / b1 = " + w.divideFloats(a1, b1));
float a2 = 15, b2 = 0; //diviision will result in infinity
System.out.println("a2 / b2 = " + w.divideFloats(a2, b2));
}
}


output
a + b = 9
a1 / b1 = 3.75
a2 / b2 = null

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote