Chapter 2 Assignments. Ensure all class names are exactly what I ask for. 1) Create a class called Printer with a main method. Make it print the following output to the console, but only use one line...

1 answer below »
Here is the assignments. Lab 2 solution is a walkthrough of Lab 2 assignment. To be done in IDE eclipse with a .java format eclipse.org/downloadsEclipse IDE for Java Developers.Go to Window -> Preferences -> Java -> Compilera.Check “Use default compliance settings”with JDK 1.8,Apply, OK.



Chapter 2 Assignments. Ensure all class names are exactly what I ask for. 1) Create a class called Printer with a main method. Make it print the following output to the console, but only use one line of code to do this (hint, make use of slide 2-24). \\Birds: Robin Owl Duck\\ 2) Create a class called Mather with a main method. Create an int width = 7. Create an int height = 6. Create the output below. Use Java arithmetic, do not just type in the numbers (hint, make use of slide 2-59 and use parenthesis for math if you need to). While not required, try to use String constants instead of raw text for output (ie, “The area is: “). You can do this assignment iteratively. First, create that output. Then, extract each text message into a String, making sure the program still works correctly. Output: The area is: 42 Width plus height is: 13 Width modulus height is: 1 Width divided by height is: 1 3) Create a class called Input with a main method. Slide 2-47 + 2-82: Scanner and char: Adapt lab part 3 to be char instead of integer. Ask for char instead. Tell the Scanner to read a char instead. Set the value to a char instead of an integer. Use this line to get a char from the Scanner: char inputChar = scanner.next().charAt(0); Explanation: next() gets the next String from the Scanner. charAt(0) gets the first char in that String. Please submit all 3 .java files. Part 1 – 3 points Part 2 – 3 points Part 3 – 4 points Remember: All code goes inside of the body of the main method (inside the { }) 1) Slide 2-25 – Print this to the screen, as it appears (not the image): Our favorite pets are: Cat Dog And catdog! 2) Slide 2-68 + 2-72 – constants – create a final String constant called “CAT” and set it to “Cat”. In your print statement, replace “Cat” with the new variable, CAT. If time permits, do this with Dog and catdog as well. In the real world, we don’t use raw text in an output. Every output is a variable or combination of variables. (Such as: final String FAVORITE_PETS = “Our favorite pets are:”) 3) Slide 2-82 – Scanner - Create a new class with a main method for this exercise. First, we’re going to create some constants (so we can avoid using raw text in outputs  ): final String ASK_FOR_INT = "Enter input integer: "; final String INTEGER = "Integer: "; Now, create a scanner: Scanner scanner = new Scanner(System.in); (Please note that with the Save Actions set, when you save the file in Eclipse, Scanner is automatically imported in the file. At the top of the Java file, you will see: import java.util.Scanner; If it doesn’t automatically import it, you can use CTRL+SHIFT+O to trigger the import, or just manually put that line in) Next, ask the user for an integer: System.out.println(ASK_FOR_INT); Next, read an integer from the Scanner (this will wait for the user to input an integer): int input = scanner.nextInt(); Now, we will output the input integer to the screen: System.out.println(INTEGER + input); Lastly, remember to always close the Scanner: scanner.close(); Run it! In the console, it will ask you for to enter an integer. It will wait for you to type in an integer and press Enter (Return). It will then output the integer back to you, along with the text. Part 1: Use “\t” for a tab/indent feature. Part 2: I created a String constant for the word “Cat” and put it in to the sysout. The plus sign here is not mathematical addition. Since there is no math going on here, Java knows that the plus sign is the string concatenation operator. It combines two Strings. Please take this example and do the same for DOG and CATDOG. Part 3: Once you get this far, hover your mouse over the red Scanner. Whenever something is red, highlight it. Eclipse will do its best to help you solve the problem. Click to import Scanner from java.util. This means that your class is now using the Scanner class. This import statement should now be there. Remember to SAVE often (CTRL+S, File->Save, whatever). The more often you SAVE while coding, the less problems you will face. This asterisk indicates that a file is NOT SAVED. Cue to SAVE your file. Continue to code from the lab: Now we can see that the variable scanner is yellow. We can always hover our mouse over to see what the problem is. Last step of the lab, on the last page: close the scanner! Always close the scanner. In all future assignments, remember to close the scanner. If you don’t close the scanner, you are creating a resource leak (which is not in the scope of this course, but worth Googling for more knowledge). Last bit of code: Now that we’re done coding, it’s time to run it. Your console should come up with this message. This is the String we set called ASK_FOR_INT. It’s simple a user prompt asking the user to do something. Who is the user here? You are! Go ahead and type in a number, then press Enter.
Answered Same DayFeb 01, 2021

Answer To: Chapter 2 Assignments. Ensure all class names are exactly what I ask for. 1) Create a class called...

Arun Shankar answered on Feb 02 2021
137 Votes
public class Mather
{
    public static void main(String[] args)
    {
        int width=7, height=6;
        Sys
tem.out.println("The area is: "+(width*height));
        System.out.println("Width plus height is: "+(width+height));
        System.out.println("Width modulus height is:...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here