In: Computer Science
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.
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: