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...
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...
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;...
Write in java please Instructions Your goal is to take N integer inputs from the user...
Write in java please Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
PLEASE CHECK MY ANSWERS! (1 pt each) If the following generally apply to all prokaryotes, write...
PLEASE CHECK MY ANSWERS! (1 pt each) If the following generally apply to all prokaryotes, write ‘P’. If they apply specifically to eukaryotes, write ‘E”. If they apply primarily to bacteria, write ‘B’, and if they apply primarily to archaea, write ‘A’. If it applies to more than one, include letters it applies to. 1 point each. ___P____6. DNA is organized in usually one large, circular chromosome. ____E___7. Cells contain membrane-bound organelles. ____P&B___8. Cell walls are common and contain some...
The title of the course is OS. All answers should be based on that. Please do...
The title of the course is OS. All answers should be based on that. Please do not copy and paste answers on chegg or on google for me. All answers should be based on your understanding on the course. Please try as much to answer the questions based on what is asked and not setting your own questions and answering them. Let it be if you want copy and paste answers. ********************************************************************************************************************************************************************************************************************** (1) Operating systems protect the computer hardware and...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT