In: Computer Science
Write in java
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.
/* As per guidelines I am not required to answer all the question given. So I am answering as much as I can in the given time frame*/
QUESTION 1 LETTER INDEX:
LetterIndex.java
import java.util.*;
class LetterIndex{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine(); // Takes string input
input=input.trim();
String word=input.substring(0,input.indexOf(" ")); //Separates the word
input=input.substring(input.indexOf(" ")+1);
String indexes[]=input.split(" "); // Seperates the Indexes into array
for(int i=0;i<indexes.length;i++)
{
int index=Integer.parseInt(indexes[i]); // Converts them to integer
if(index>=word.length()) // Checks index value if greater then breaks
break;
else
System.out.println(word.charAt(index)); // Else prints the character
}
}
}
OUTPUT TO LETTER INDEX:
QUESTION 2 MATCHING LETTER:
MatchingLetter.java
import java.util.*;
class MatchingLetter{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine(); // Taking Input
input=input.trim();
String words[]=input.split(" "); // Separating the words
String word1,word2;
// Storing the small word in word1 and large word in word2
if(words[0].length()<words[1].length())
{
word1=words[0];
word2=words[1];
}
else
{
word1=words[1];
word2=words[0];
}
// Looping through the small word to find similar characters
for(int i=0;i<word1.length();i++)
{
if(word1.charAt(i)==word2.charAt(i))
System.out.println(word1.charAt(i));
}
}
}
OUTPUT:
QUESTION 3 WORD COUNT:
WordCount.java
import java.util.*;
class WordCount{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String word=sc.nextLine(); //Taking input word
word=word.trim();
String sentence=sc.nextLine(); // Taking input sentence
sentence=sentence.trim();
int count=0;
String words[]=sentence.split(" "); // Spilitting the words in the sentece into the array
for(int i=0;i<words.length;i++)
{
if(words[i].equals(word)) // checking for equal word
count++;
}
System.out.println(count); // Printing the count
}
}
OUTPUT:
QUESTION 7 PALINDROME NUMBERS:
PalindromeNumbers.java
import java.util.*;
class PalindromeNumbers{
public static boolean isPal(int n)
{
/* To check if a number is palindrome we could reverse the number and check it with the original number*/
int copy=n; // Making copy
int reverse=0; // To Store the reverse
while(n>0)
{
int r=n%10; // Extracting the last digit using modulo
reverse=reverse*10+r; // Adding the digit to the end of the number
n=n/10; // removing the last digit by dividing by 10
}
return reverse==copy;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine(); // Taking the input
input=input.trim();
// Converting them to integers
int begin=Integer.parseInt(input.substring(0,input.indexOf(' ')));
int end=Integer.parseInt(input.substring(input.indexOf(' ')+1));
for(int i=begin;i<=end;i++)
{
if(isPal(i)) // caliing the palindrome function
System.out.println(i); // Printing if palindrome
}
}
}
OUTPUT: