Docsity

Java Programming Exercises with Answers: Introduction to Problem Solving and Programming, Exams of Nursing

Toggle sidebar
bg1
Java an Introduction to Problem Solving
and Programming Chapter 1 (Exercises
with answers)
How does a computer's main memory differ from its auxiliary
memory? - answer ✅✅All information in Main Memory will
disappear when the computer shuts down, when all
information that is stored in auxiliary memory will stay even
after the computer shuts down.
After you use a text editor to write a program, will it be in
main memory or auxiliary memory? - answer ✅✅Auxiliary
Memory
When a computer executes a program, will it be in main
memory or auxiliary memory? - answer ✅✅Main Memory
How does machine language differ from Java? - answer
✅✅Machine code works in low-level code, where Java works
with high-level code.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Programming Exercises with Answers: Introduction to Problem Solving and Programming and more Exams Nursing in PDF only on Docsity!

Java an Introduction to Problem Solving

and Programming Chapter 1 (Exercises

with answers)

How does a computer's main memory differ from its auxiliary memory? - answer ✅✅All information in Main Memory will disappear when the computer shuts down, when all information that is stored in auxiliary memory will stay even after the computer shuts down. After you use a text editor to write a program, will it be in main memory or auxiliary memory? - answer ✅✅Auxiliary Memory When a computer executes a program, will it be in main memory or auxiliary memory? - answer ✅✅Main Memory How does machine language differ from Java? - answer ✅✅Machine code works in low-level code, where Java works with high-level code.

How does bytecode differ from machine language? - answer ✅✅bytecode consists of 1's and 0's, where machine language works with a set of actions. What would the following statements, when used in a Java program, display on the screen? int age; age = 20; System.out.println ("My age is"); System.out.println(age); - answer ✅✅My age is 20 Write a statement or statements that can be used in a Java program to display the following on the screen: 3 2 1 - answer ✅✅public class Count { public static void main(String[] args){ System.out.println(3); System.out.println(2);

Give a person's year of birth, the Birthday Wizard can compute the year in which the person's nth birthday will occur or has occurred. Write statements that can be used in a Java program to perform this computation for the Birthday Wizard. - answer ✅✅import java.util.Scanner; public class BirthdayWizard { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Please supply year of birth:"); int year = scan.nextInt(); System.out.println("\nTo see at what year you would be what age, please supply your age:"); int age = scan.nextInt(); int sum = year + age; System.out.println("\nWhen you are " + age + " , the year will be: " + sum);

Write statements that can be used in a Java program to read two integers and display the number of integers that lie between them, including the integers themselves. For example, four integers are between 3 and 6: 3, 4, 5, and 6. - answer ✅✅import java.util.Scanner; public class UserCount { public static void main(String[] args){ int backVal1; int backVal2; Scanner scan = new Scanner(System.in); System.out.println("This program will take two separate values and add the missing values in between");

A single bit can represent two values: 0 and 1. Two bits can represent four values: 00, 01, 10, and 11. Three bits can represent eight values: 000, 001, 010, 011, 100, 101, 110, and

  1. How many values can be represented by a. 8 bits? b. 16 bits? c. 32 bits? - answer ✅✅8 bits = 256 values. 16 bits = 65 536 values. 32 bits = 4 294 967 296 values. Find the documents from the Java Class Library on the Oracle Web Site. (At this writing, the link to this documentation is http://docs.oracle.com/javase/7/docs/api/.) Then find the description for the class Scanner. How many methods are described in the section entitled "Method Summary"? - answer ✅✅ 55

Self-Test Question 27 asked you to think of some attributes for a song object. What attributes would you want for an object that represents a play list containing many songs? - answer ✅✅• Total song amount

  • Song Length
  • A way to sort the songs
  • Set order of songs What behaviors might a song have? What behaviors might a play list have? Contrast the difference in behavior between the two kinds of objects. - answer ✅✅Song behaviors:
  • Play/Pause
  • Stop
  • Previous
  • Next Play list behaviors:
  • Select random song
  • Place list in a certain order
  • Highlight current song
  • Move song up or down play list Behavior differences:

subtract 1 from k Write an algorithm that finds the maximum value in a list of values. - answer ✅✅maxValue = - 1 iterate through each list value { if list value is greater than maxValue then maxValue = list value } Write statements that can be used in a Java applet to draw the five interlocking rings that are the symbol of the Olympics. (Don't worry about the color.) - answer ✅✅import javax.swing.JApplet; import java.awt.Graphics; public class Olympics extends JApplet{ public void paint(Graphics g){ g.drawOval(50, 50, 100, 100); g.drawOval(110, 100, 100, 100); g.drawOval(170, 50, 100, 100); g.drawOval(230, 100, 100, 100);

g.drawOval(290,50, 100, 100); } } Find the documentation for the class Graphics in Java Class Library. (See Exercise 12.) Learn how to use the method drawRect. Then write statements that can be used in a Java applet to draw a square containing a circle. The circle's diameter and the square's side should be equal in size. - answer ✅✅import javax.swing.JApplet; import java.awt.Graphics; public class DrawSquare extends JApplet{ public void paint(Graphics g){ g.drawRect(0, 0, 200, 200); g.drawOval(0, 0, 200, 200); } }