Question
Lab - Chapter 9 Introduction to Wrapper classes, this is a tool or concept provided in the Java Programming Language that gives us a way
Lab - Chapter 9
Introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods.
A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object and object into primitive.
Working with collections, we use generally generic ArrayList
Advantages of Java Wrapper Class
- to convert the primitive data types into objects (Objects are needed when we need to pass an argument in the given method).
- The package java.util contains classes which only handles objects, so it helps in this case too.
- Data Structures store only objects and primitive data types.
- In multithreading, we need an object to support synchronization.
Primitive Data Type Wrapper Class byte Byte long Long float Float int Int
1. Use a Word Counter application, submit WordCounter.java.
Word Counter takes input of a text file name (have a sample file in the same directory as your Word Counter application to we don't have to fully path the file). The output of our Word Counter application is to echo the file name and the number of words contained within the file.
Consult Code Listing 9-9 and 9-10 for examples of analyzing data in a file. The Code listings use Exceptions.
import java.util.Scanner;
public class javaCalculator
{
public static void main(String[] args)
{
int num1;
int num2;
String operation;
Scanner input = new Scanner(System.in);
//Get input from user
System.out.println("please enter the first number");
num1 = input.nextInt();
System.out.println("please enter the second number");
num2 = input.nextInt();
Scanner op = new Scanner(System.in);
System.out.println("Please enter operation");
operation = op.next();
// string comparison operator to determine which math operation to perform
if (operation.equals("+"))
{
System.out.println("your answer is " + (num1 + num2));
}
else if (operation.equals("-"))
{
System.out.println("your answer is " + (num1 - num2));
}
else if (operation.equals("/"))
{
System.out.println("your answer is " + (num1 / num2));
}
else if (operation.equals("*"))
{
System.out.println("your answer is " + (num1 * num2));
}
else
{
System.out.println("illegal operation");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started