All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
building java programs a back to basics approach
Questions and Answers of
Building Java Programs A Back to Basics Approach
Add the following accessor method to the Point class:public double slope(Point other)Returns the slope of the line drawn between this Point and the given other Point. Use the formula (y2 − y1) /
Imagine that you are creating a class called Calculator. A Calculator object could be used to program a simple mathematical calculator device like the ones you have used in math classes in school.
Add the following accessor method to the Point class:public boolean isVertical(Point other)Returns true if the given Point lines up vertically with this Point, that is, if their -coordinates are the
What is the output of the following program?
Add the following accessor method to the Point class:public int manhattanDistance(Point other)Returns the “Manhattan distance” between the current Point object and the given other Point object.
What is the state of a String object? What is its behavior?
Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given
Add the following mutator method to the Point class:public void flip()Negates and swaps the x/y coordinates of the Point object. For example, if an object pt initially represents the point (5, -3),
What is an object? How is an object different from a class?
Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date
Add the following accessor method to the Point class:public int quadrant()Returns which quadrant of the x/y plane the current Point object falls in. Quadrant 1 contains all points whose x and y
Describe the difference between object-oriented programming and procedural programming.
Write a class called RationalNumber that represents a fraction with an integer numerator and denominator. A RationalNumber object should have the following methods:public RationalNumber(int
Write a program to score users’ responses to the classic Myers–Briggs personality test. Assume that the test has 70 questions that determine a person’s personality in four dimensions. Each
Write code that creates an array of integers named data of size 5 with the following contents: [0] [1] [2] [3] [4] data 27 51 33 -1 101
What would be the output from the code in the previous exercise if the calls to hasNextLine and nextLine were replaced by calls to hasNext and next, respectively?Data from Previous ExerciseFor the
Write a method called stripHtmlTags that accepts a Scanner representing an input file containing an HTML web page as its parameter, then reads that file and prints the file’s text with all HTML
Write a program that plays a game where a player is asked to fill in various words of a mostly complete story without being able to see the rest. Then the user is shown his/her story, which is often
Write a method called showTwos that shows the factors of 2 in a given integer. For example, consider the following calls:showTwos(7);showTwos(18);showTwos(68);showTwos(120);These calls should produce
For each of the following while loops, state how many times the loop will execute its body. Remember that “zero,” “infinity,” and “unknown” are legal answers. Also, what is the output of
Write an interactive program that reads lines of input from the user and converts each line into “Pig Latin.” Pig Latin is English with the initial consonant sound moved to the end of each word,
Write a method called gcd that accepts two integers as parameters and returns the greatest common divisor (GCD) of the two numbers. The GCD of two integers a and b is the largest integer that is a
Convert each of the following for loops into an equivalent while loop:a.b.c.d. for (int n = 1; n
Write a reverse Hangman game in which the user thinks of a word and the computer tries to guess the letters in that word. The user tells the computer how many letters the word contains.
Write a method called toBinary that accepts an integer as a parameter and returns a String containing that integer’s binary representation. For example, the call of printBinary(44) should return
Write a program that plays a guessing game with the user. The program should generate a random number between 1 and some maximum (such as 100 ), then prompt the user repeatedly to guess the number.
Write a method called randomX that prints a lines that contain a random number of “x” characters (between 5 and 20 inclusive) until it prints a line that contains 16 or more characters. For
Consider the following method:For each of the following calls, indicate the output that the preceding method produces:a. mystery(19);b. mystery(42);c. mystery(48);d. mystery(40);e. mystery(64);
Write a program that plays a reverse guessing game with the user. The user thinks of a number between 1 and 10 , and the computer repeatedly tries to guess it by guessing random numbers. It’s fine
Write a method called randomLines that prints between 5 and 10 random strings of letters (between “a” and “z”), one per line. Each string should have random length of up to 80 characters.
Consider the following code:Random rand = new Random();int a = rand.nextInt(100);int b = rand.nextInt(20) + 50;int c = rand.nextInt(20 + 50);int d = rand.nextInt(100) – 20;int e = rand.nextInt(10)
Write a game that plays many rounds of Rock Paper Scissors. The user and computer will each choose between three items: rock (defeats scissors, but loses to paper), paper (defeats rock, but loses to
Write a method called makeGuesses that guesses numbers between 1 and 50 inclusive until it makes a guess of at least 48 . It should report each guess and at the end should report the total number of
Write code that generates a random integer between 0 and 10 inclusive.
Write a program that draws a graphical display of a 2D random walk using a DrawingPanel. Start a pixel walker in the middle of the panel. On each step, choose to move 1 pixel up, down, left, or
Write a method called diceSum that accepts a Scanner for the console as a parameter and prompts for a desired sum, then repeatedly simulates the rolling of 2 six-sided dice until their sum is the
Write code that generates a random odd integer (not divisible by 2) between 50 and 99 inclusive.
Write a program that plays the dice game “Pig.” Pig is a two-player game where the players take turns repeatedly rolling a single 6-sided die; a player repeatedly rolls the die until one of two
Write a method called randomWalk that performs steps of a random one-dimensional walk. The random walk should begin at position 0. On each step, you should either increase or decrease the position by
For each of the do/while loops that follow, state the number of times that the loop will execute its body. Remember that “zero,” “infinity,” and “unknown” are legal answers. Also, what is
Write a method called printFactors that accepts an integer as its parameter and uses a fencepost loop to print the factors of that number, separated by the word "and ". For example, the factors of
Write a do/while loop that repeatedly prints a certain message until the user tells the program to stop. The do/while is appropriate because the message should always be printed at least one time,
Write a method called hopscotch that accepts an integer number of “hops” as its parameter and prints a pattern of numbers that resembles a hopscotch board. A “hop” is a three-number sequence
Write a method called zeroDigits that accepts an integer parameter and returns the number of digits in the number that have the value 0. For example, the call zeroDigits(5024036) should return 2, and
Write a method called threeHeads that repeatedly flips a coin until the results of the coin toss are three heads in a row. You should use a Random object to make it equally likely that a head or a
Write a do/while loop that repeatedly prints random numbers between 0 and 1000 until a number above 900 is printed. At least one line of output should always be printed, even if the first random
Write a method called printAverage that uses a sentinel loop to repeatedly prompt the user for numbers. Once the user types any number less than zero, the method should display the average of all
Consider the flawed method printLetters that follows, which accepts a String as its parameter and attempts to print the letters of the String , separated by dashes. For example, the call of
Write a method called consecutive that accepts three integers as parameters and returns true if they are three consecutive numbers—that is, if the numbers can be arranged into an order such that,
Write a sentinel loop that repeatedly prompts the user to enter a number and, once the number –1 is typed, displays the maximum and minimum numbers that the user entered. Here is a sample
Write a method called hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly
Consider the following variable declarations:int x = 27;int y = –1;int z = 32;boolean b = false;What is the value of each of the following Boolean expressions?a. !bb. b || truec. (x > y)
Write a method called dominant that accepts three integers as parameters and returns true if any one of the three integers is larger than the sum of the other two integers. The integers might be
Write a method called isVowel that accepts a character as input and returns true if that character is a vowel (a, e, i, o, or u). For an extra challenge, make your method case-insensitive.
Write a method called anglePairs that accepts three angles (integers), measured in degrees, as parameters and returns whether or not there exist both complementary and supplementary angles among the
The following code attempts to examine a number and return whether that number is prime (i.e., has no factors other than 1 and itself). A flag named prime is used. However, the Boolean logic is not
Write a method called monthApart that accepts four integer parameters, m1, d1, m2, and d2, representing two calendar dates. Each date consists of a month (1 through 12) and a day (1 through the
The following code attempts to examine a String and return whether it contains a given letter. A flag named found is used. However, the Boolean logic is not implemented correctly, so the method does
Using “Boolean Zen,” write an improved version of the following method, which returns whether the given String starts and ends with the same character: public static boolean startEndSame (String
Write a method called digitSum that accepts an integer as a parameter and returns the sum of the digits of that number. For example, the call digitSum(29107) returns 2 + 9 + 1 + 0 + 7 or 19. For
Write a method called firstDigit that returns the first (most significant) digit of an integer. For example, firstDigit(3572) should return 3. It should work for negative numbers as
Using “Boolean Zen,” write an improved version of the following method, which returns whether the given number of cents would require any pennies (as opposed to being an amount that could be made
Write a method called digitRange that accepts an integer as a parameter and returns the range of values of its digits. The range is defined as 1 more than the difference between the largest and
Consider the following method:For each of the following calls, indicate the value that is returned:a. mystery(3, 3)b. mystery(5, 3)c. mystery(2, 6)d. mystery(12, 18)e. mystery(30, 75) public static
Write a method called swapDigitPairs that accepts an integer as a parameter and returns a new integer whose value is similar to ’s but with each pair of digits swapped in order. For example, the
The following code is a slightly modified version of actual code that was in the Microsoft Zune music player in 2008. The code attempts to calculate today’s date by determining how many years and
Write a method called allDigitsOdd that returns whether every digit of a positive integer is odd. Return true if the number consists entirely of odd digits (1, 3, 5, 7, 9) and false if any of its
Which of the following is a properly reversed version of the following Boolean expression, according to De Morgan’s Laws?(2 == 3) && (–1 < 5) && isPrime(n)a. (2 != 3)
Consider the following code:What is the output when the user types the following values?a. Janeb. 56c. 56.2 Scanner console new Scanner (System.in); !! System.out..print ("Type something for me! ");
Write a method called isAllVowels that returns whether a string consists entirely of vowels (a, e, i, o, or u, case-insensitively). If and only if every character of the string is a vowel, your
Write a method called boyGirl that accepts a Scanner that is reading its input from a file containing a series of names followed by integers. The names alternate between boys’ names and girls’
Students are often asked to write term papers containing a certain number of words. Counting words in a long paper is a tedious task, but the computer can help. Write a program that counts the number
What is a file? How can we read data from a file in Java?
Write a method called evenNumbers that accepts a Scanner reading input from a file with a series of integers, and report various statistics about the integers to the console. Report the total number
Write a program that compares two files and prints information about the differences between them. For example, consider a file data1.txt with the following contents:This file has a great deal of
What is wrong with the following line of code?Scanner input = new Scanner("test.dat");
Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of integers, and print a message to the console indicating whether the sum starting from the
Write a program that prompts the user for a file name, assuming that the file contains a Java program. Your program should read the file and print its contents properly indented. When you see a
Which of the following is the correct syntax to declare a Scanner to read the file example.txt in the current directory?a. Scanner input = new Scanner("C:\example.txt");b. Scanner input = new
Write a method called countCoins that accepts a Scanner representing an input file whose data is a series of pairs of tokens, where each pair begins with an integer and is followed by the type of
Write a program that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name
Write code to construct a Scanner object to read the file input.txt, which exists in the same folder as your program.
Write a method called collapseSpaces that accepts a Scanner representing an input file as its parameter, then reads that file and outputs it with all its tokens separated by single spaces, collapsing
Given the following line of input, what tokens does a Scanner break the line apart into?welcome...to the matrix.a. "welcome", "to", "the", "matrix"b. "welcome...to the matrix."c. "welcome...to",
Write a method called readEntireFile that accepts a Scanner representing an input file as its parameter, then reads that file and returns its entire text contents as a String.
Given the following lines of input, what tokens does a Scanner break the line apart into?in fourteen-hundred 92 columbus sailed the ocean blue :)a. "in", "fourteen-hundred", "92"b. "in",
Write a method called flipLines that accepts a Scanner for an input file and writes to the console the same file’s contents with each pair of lines reversed in order. If the file contains an odd
How many tokens are there in the following input, and what Scanner method(s) can be used to read each of the tokens?Hello there,how are you?I am "very well", thank you.12 34 5.67 (8 + 9) "10"
Write a method called doubleSpace that accepts a Scanner for an input file and a PrintStream for an output file as its parameters, writing into the output file a double-spaced version of the text in
What is wrong with the following line of code?Scanner input = new Scanner(new File("C:\temp\new files\test.dat"));
Write a method called wordWrap that accepts a Scanner representing an input file as its parameter and outputs each line of the file to the console, word-wrapping all lines that are longer than 60
Answer the following questions about a Java program located on a Windows machine in the folder C:\Documents and Settings\amanda\My Documents\programs:a. What are two legal ways you can refer to the
Modify the preceding wordWrap method so that it outputs the newly wrapped text back into the original file. (Be careful—don’t output into a file while you are reading it!) Also, modify it to use
Answer the following questions about a Java program located on a Linux machine in the folder /home/amanda/Documents/hw6:a. What are two legal ways you can refer to the
Modify the preceding wordWrap method so that it only wraps whole words, never chopping a word in half. Assume that a word is any whitespace-separated token and that all words are under 60 characters
The following program contains 6 mistakes! What are they? public class Oops6 { public static void main (String [] args) { 3 Scanner in = new Scanner ("example.txt"); 4 countWords (in); 7 // Counts
For the next several questions, consider a file called readme.txt that has the following contents:What would be the output from the following code when it is run on the readme.txt file? 6.7 This file
Write a method called stripComments that accepts a Scanner representing an input file containing a Java program as its parameter, reads that file, and then prints the file’s text with all comments
Write a method called printDuplicates that takes as a parameter a Scanner containing a series of lines. Your method should examine each line looking for consecutive occurrences of the same token on
What would be the output from the code in the previous exercise if the calls to hasNextLine and nextLine were replaced by calls to hasNextInt and nextInt, respectively? How about hasNextDouble and
Showing 600 - 700
of 1041
1
2
3
4
5
6
7
8
9
10
11