I308 C307 Data/Information Representation

I308 C307 Lab 1

Due Date: Tuesday, January 18, 2022.

In this lab, we will create a Java class that inputs an integer from the user and converts it to binary. For this, it will use an array.

Ex. 1. a. Declaring a Class.

Open either Eclipse or NetBeans.

Installing an IDE. If you don't have either Eclipse or Netbeans installed on your computer, here are the links to download them:

Eclipse: https://www.eclipse.org/downloads/

NetBeans: https://netbeans.apache.org/download/

Beside these IDEs, you may also have to update Java the latest version, and also install the Java SE Development kit:
https://www.oracle.com/java/technologies/javase-downloads.html

Create Project Create a project called Lab1. Inside this project, create a class called Converter containing the main function. Make sure that the class is in a package called lab1.

b. Class Attributes and Methods

Add an attribute for the number in decimal notation, called decimal, and an array of 100 integer elements called binary. In addition, declare an integer attribute for the size of the binary array.

Declare a public constructor with no parameters (default constructor). Add a method that outputs the data in the class (all the attributes and the binary array in reverse).

c. Conversion Method

Add a public method called convert with an integer parameter n. This method should store the number n in the decimal attribute, convert the number into binary using the binary array, and call the function to output the result.

For the conversion, use an index in the binary array starting from 0. First check if the number to convert is odd or even. If it's odd, store 1 in the binary array at the index, and if not, store a 0. Then increase the index and divide the number by 2. Repeat this until the number becomes 0. The last value of the index should be stored as the size of the binary array.

In the main, declare a Scanner object for easier input. For this, at the top of the file, inside the package but before the class, add a statement to import the module java.util.Scanner. Then add a variable called scan of type Scanner and initialize it with new Scanner(System.in).

Declare a variable of type converter and initialize it with a new object. Create a little interface menu with two options: 1 to convert a number to binary, and 2 to quit. For 1, ask the user to enter an integer number and convert it to binary and output the result. For 2, quit the program. Repeat this until the user enters 2. You can use a boolean variable quit to setup the interface loop.

This program will be continued in the homework.

Lab Submission

Upload the file Converter.java in Canvas, Assignments, Homework 1. You can wait until you finish the homework to upload all files at the same time. Since the homework continues the same program as in the lab, you don't need to submit the lab file separately.