Question

In: Computer Science

Write in java The submission utility will test your code against a different input than the...

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.

Solutions

Expert Solution

/* 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:


Related Solutions

Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of Eclipse showing output of program. (JPG or PNG format only) Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards. Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Write a java code that gets student test scores from the user. Each test score will...
Write a java code that gets student test scores from the user. Each test score will be an integer in the range 0 to 100. Input will end when the user enters -1 as the input value. After all score have been read, display the number of students who took the test, the minimum score, the maximum score, the average score (with decimal point, and the number of A where an A is a score in the range 90-100.
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
Write a java code that allows the user to input any word/name from the console (10...
Write a java code that allows the user to input any word/name from the console (10 words/names only) and then sorts and alphabetizes them into an array without using predefined methods (.compareTo(), or any sorter functions) and then print out the results. you must have three methods that can: Compare multiple strings Swap elements Sort and alphabetize string ***REMEMBER DO NOT USE ANY PRE-DEFINED METHODS*** Example of how the code should work: Enter names/words (10 only): "james, john, alex, aaron,...
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...
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT