Question

In: Computer Science

Instructions: Please write all answers in java Each problem should be completed as a single separate...

Instructions:

Please write all answers in java

Each problem should be completed as a single separate .java file, each with its own main(). Inputs should be read using a Scanner object and output should be printed using System.out.println. As you finish each question, submit your code to the autograder at:

http://162.243.28.4/grader/homework2.html

Make sure to include your name at the top as a single word.

The submission utility will test your code against a different input than the sample given.

When you're finished, upload all of your .java files to Blackboard.

Grading:

Each problem will be graded as follows:

0 pts: no submission

1 pts: submitted, but didn't compile

2 pts: compiled, but didn't produce the right output

5 pts: compiled and produced the right output

Problem 1: "Letter index"

Write a program that inputs a word and an unknown number of indices and prints the letters of the word corresponding to those indices. If the index is greater than the length of the word, you should break from your loop

Sample input:

apple 0 3 20

Sample output:

a l

Problem 2: "Matching letters"

Write a program that compares two words to see if any of their letters appear at the same index. Assume the words are of equal length and both in lower case. For example, in the sample below, a and e appear in both words at the same index.

Sample input:

apple andre

Sample output

a

e

Problem 3: "Word count"

You are given a series of lowercase words separated by spaces and ending with a . , all one on line. You are also given, on the first line, a word to look up. You should print out how many times that word occured in the first line.

Sample input:

is

computer science is no more about computers than astronomy is about telescopes .

Sample output:

2

Problem 4: "Treasure Chest"

The input to your program is a drawing of a bucket of jewels. Diamonds are represented as @, gold coins as $, rubies as *. Your program should output the total value in the bucket, assuming diamonds go for $1000, gold coins for $500, and rubies for $300. Note that the bucket may be wider or higher than the bucket in the example below.

Sample input:

|@* @ |

| *@@*|

|* $* |

|$$$* |

| *$@*|

-------

Sample output:

$9900

Problem 5: “Speed Camera”

Speed cameras are devices that monitor traffic and automatically issue tickets to cars going above the speed limit. They work by comparing two pictures of a car at a known time interval. If the car has traveled more than a set distance in that time, the car is given a citation.

The input are two text representations of a traffic picture with a car labeled as letters “c” (the car is moving upwards. These two pictures are shot exactly 1 second apart. Each row is 1/50 of a mile. The car is fined $10 for each mile per hour over 30 mph, rounded down to the nearest mph. Print the fine amount.

Sample input:

|.|

|.|

|.|

|.|

|c|

---

|.|

|c|

|.|

|.|

|.|

Sample output:

$1860

Problem 6. Distance from the science building

According to Google Maps, the DMF science building is at GPS coordinate 41.985 latitude, -70.966 longitude. Write a program that will read somebody’s GPS coordinate and tell whether that coordinate is within one-and-a-half miles of the science building or not.

Sample input:

-70.994

41.982

Sample output:

yes

At our position, 1 1/2 miles is about .030 degrees longitude, but about .022 degrees latitude. That means that you should calculate it as an ellipse, with the east and west going from -70.936 to -70.996, and the north and south going from 41.963 to 42.007.

Hint: Use the built in Ellipse2D.Double class. Construct a Ellipse2D.Double object using the coordinates given, and then use its "contains" method.

Problem 7: "Palindrome Numbers"

A palindrome is a word that reads the same forwards and backwards, such as, for example, "racecar", "dad", and "I". A palindrome number is the same idea, but applied to digits of a number. For example 1, 121, 95159 would be considered palindrome numbers.

The input to your program are two integers start and end. The output: all of the palindrome numbers between start and end (inclusive), each on a new line.

Sample input:

8 37

Sample output:

8

9

11

22

33

Hints:

1. Start by writing and testing a function that takes a number and returns true/false if the number is a palindrome. Then call that function in a for loop.

2. To see if a number is a palindrome, try turning it into a string. Then use charAt to compare the first and last digits, and so on.

Solutions

Expert Solution

1) Java program for Letter Index

Code:

//importing Scanner to read input
import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            //declaring variables
            String line, word;
            String[] input;
            //reading input
        line=sc.nextLine();
        //separating word and indices
        input=line.trim().split("\\s+");
        word=input[0];
        int index;
        //iterating till number of indices entered as input
        for(int i=1;i<input.length;i++) {
            index=Integer.parseInt(input[i]);
            //if index is greater than word length, breaking loop
            if(index>=word.length()){
                break;
            }
            //printing letters of word at the given index
            System.out.print(word.charAt(index)+" ");
        }
        }
}

Code Screenshot:

Output:

2) Java program for Matching letters

Code:

//importing Scanner to read input
import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            //declaring variables
            String line, word1, word2;
            String[] input;
            //reading input
        line=sc.nextLine();
        //separating words
        input=line.trim().split("\\s+");
        word1=input[0];
        word2=input[1];
        //iterating till number of letters in each word
        for(int i=0;i<word1.length();i++) {
            //if letter is equal in both words, printing the letter0
            if(word1.charAt(i)==word2.charAt(i)){
                System.out.println(word1.charAt(i));
            }
        }
        }
}

Code Screenshot:

Output:

3) Java program for Word Count

Code:

//importing Scanner to read input
import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            //declaring variables
            String line, word;
            String[] input;
            //initializing count to 0
            int count=0;
            //reading input
            word=sc.nextLine();
        line=sc.nextLine();
        //separating word and indices
        input=line.trim().split("\\s+");
        //iterating till number of words in the given line
        for(int i=0;i<input.length;i++) {
            //if word is in the line, increasing count
            if(input[i].equals(word)){
                count++;
            }
        }
        //printing count
        System.out.print(count);
        }
}

Code Screenshot:

Output:

7) Java program for Palindrome Numbers

Code:

import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
                //declaring variables
                int start, end, num, reverse=0, remainder;
        String line;
            String[] input;
            //reading input
        line=sc.nextLine();
        //separating start and end number
        input=line.trim().split("\\s+");
        //assigning start and end
        start=Integer.parseInt(input[0]);
        end=Integer.parseInt(input[1]);
        //looping from start value till end value
        for(int i=start;i<=end;i++){
            num=i;
            //finding the reverse of the current number
            while(num!=0)
            {
                remainder=num%10;
                reverse=reverse*10+remainder;
                num/=10;
            }
            //checking if current number and its reverse are equal
            if(i==reverse){
                //if yes, printing the number
                System.out.println(i);
            }
            reverse=0;
        }
        }
}

Code Screenshot:

Output:


Related Solutions

Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Instructions: Please read problem carefully. Write the answers on the answer sheets provided. Make sure that...
Instructions: Please read problem carefully. Write the answers on the answer sheets provided. Make sure that you clearly show all of the work that went in to solving the problem. Partial credit will be awarded on each part of the problem, so make sure that you attempt each part. You are the marketing manager for TotalControl, an electronics company in Orangeburg. Your company produces a line of remote controls for use in home theaters. The company’s main product, the UR...
Complete each problem on a separate worksheet in a single Excel file. Rename the separate worksheets...
Complete each problem on a separate worksheet in a single Excel file. Rename the separate worksheets with the respective problem number. You may have to copy and paste the datasets into your homework file first. Name the file with your last name, first initial, and HW #2. Label each part of the question. When calculating statistics, label your outputs. Use the Solver add-in for these problems. For a telephone survey, a marketing research group needs to contact at least 600...
Write all your answers for this problem in a text file named aa.txt – for each...
Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer. 1.1 What type of object can be contained in a list (write the letter for the best answer)? a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list 1.2 Which statement about the debugger is not correct? a. It is a powerful tool...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000). Write a program that prompts the...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
Answer my this problem correctly.Balance sheet should be equal and all parts should be completed. The...
Answer my this problem correctly.Balance sheet should be equal and all parts should be completed. The following information is given for Aphria Farming Services Inc. for the year ended December 31, 2018. The account balances (all of which had their normal balance of debit or credit) at the beginning of 2018 (January 1, 2018) were as follows: Cash                                                             $      2,200 Accounts Payable                                        $     23,700 Accounts Receivable                                   $       4,400 Income Tax Payable                                    $    15,100 Prepaid Supplies (Feed and Straw)            $     27,800...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT