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

Introduction The idea of a class is foundational to the Java programminglanguage

ID: 3613789 • Letter: I

Question

Introduction

The idea of a class is foundational to the Java programminglanguage. Any code we write must be in the context of a class. Wesaw last lab that in typical work environments, our classes willcombine with others in order for a problem to be solved. But whathappens when...

Two programmers working on a problem givetwo separate classes the same name?!

Such a situation might arise when programmers at a companywriting software for a musical instrument manufacturer uses twoclasses named Keyboard. One class models the musicalinstrument and the other models the input device on the computer.When the programmers bring their code together, there will be anambiguity whenever the Keyboard class is used,preventing Eclipse from making an executable application.

(But don't take our word for it! Try creating two classes withthe same name.)

Thankfully, Java offers an easy solution to resolving thisambiguity. Classes are organized into packages. The ideaof putting a class in a package is not unlike putting a file in adirectory. Asking a user to open a file namedindex.html is bound to cause the user some trouble.("Which index.html? I have 27 of them on mycomputer.") But if you ask the user to openC:UsersmeDocuments and Settingswebsiteindex.html,there's a good chance the user will succeed.

Now let's see how it's done in Java...

Every class from the Java library that you have used so far inthe semester has been placed in a package by a programmer at SunMicrosystems, the developers of Java. These are the full names ofthe classes you may have used:

Packages are named using lowercase characters. Two packages arelisted above: java.lang andjava.util.

In order to reference classes in a package, you have a fewchoices:

The Javadoc documentation for the classes in the Java libraryis also organized by packages. Locate the documentation for theScanner class.

You have used or will use the Scanner class inlecture, in examples from the book, and in homework to do simpleinput. The next few pages of this lab will show you some of theother powerful functionality this class has to offer.

In the documentation for Scanner, you see thatthere are a number of ways one can construct a Scannerinstance. Thus far, we have passed in System.inwhenever we've constructed an instance. System.in isan instance of class java.io.InputStream thattypically manages keyboard input typed at the console. (It's pairedwith System.out for console output.System.err also exists and can be used likeSystem.out for error messages.)

Create a new class according to the code below.

In this code, we are trying to use Scanner withoutfully qualifying the class name or having imported it, and weshould see an error telling us so. Eclipse makes it very easy toimport classes with unqualified names. With the cursor in theeditor, hit Control-Shift-O.

Using your ScannerTest class, prompt the user forfour pieces of information and use input to retrievethe information from the keyboard. The information will be used toreplace the words in all capital letters in the form letter below.Examine the context of the capitalized words and use theappropriate method in the Scanner class to retrieve avalue of the appropriate type.

Explanation / Answer


import java.util.Scanner;


public class ScannerTest { private void insertInput(String NAME, int NUMBER_OF_KIDS,Boolean TRUE_OR_FALSE, String MY_NAME ) { System.out.println("Dear " + NAME); System.out.println("Wow, what a year. Ihope your "+ NUMBER_OF_KIDS); System.out.println("kids are well. Therumors that we are expecting another are "+ TRUE_OR_FALSE+". Untilnext year!");      System.out.println("Sincerely,"); System.out.println(MY_NAME); } public static void main(String[] args) { String name,myname; int no_kids; boolean bool;
System.out.println("what is recipient's name?"); Scanner input = new Scanner(System.in); name=input.next();
System.out.println("How many kids does the recipienthave?"); no_kids=input.nextInt();
System.out.println("You are expecting another kid? True orFalse"); bool=input.nextBoolean();
System.out.println("What is your name?"); myname=input.next(); ScannerTest st=new ScannerTest(); st.insertInput(name,no_kids,bool,myname); } }

import java.util.Scanner;


public class ScannerTest { private void insertInput(String NAME, int NUMBER_OF_KIDS,Boolean TRUE_OR_FALSE, String MY_NAME ) { System.out.println("Dear " + NAME); System.out.println("Wow, what a year. Ihope your "+ NUMBER_OF_KIDS); System.out.println("kids are well. Therumors that we are expecting another are "+ TRUE_OR_FALSE+". Untilnext year!");      System.out.println("Sincerely,"); System.out.println(MY_NAME); } public static void main(String[] args) { String name,myname; int no_kids; boolean bool;
System.out.println("what is recipient's name?"); Scanner input = new Scanner(System.in); name=input.next();
System.out.println("How many kids does the recipienthave?"); no_kids=input.nextInt();
System.out.println("You are expecting another kid? True orFalse"); bool=input.nextBoolean();
System.out.println("What is your name?"); myname=input.next(); ScannerTest st=new ScannerTest(); st.insertInput(name,no_kids,bool,myname); } }

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