Jump to content
Sign in to follow this  
Fervor

Help with Java Code, please

Recommended Posts

Hello everyone, I am stuck with these java instruction. I tried my best, but couldn't come up with the right codes. Could someone looks into this, and maybe could write the code for me, Thank in advance.

 

 

 

Programming Projects:

 

 

2. Write a program that asks the user to input a single letter and displays the corresponding digit on a telephone. The letters and digits on a telephone are grouped this way:

 

2=ABC4=GHI6=MNO8=TUV

3=DEF5=JKL7=PQRS9=WXYZ

 

Continue asking user to enter the letter until any non alphabetic character is entered. Also, the program should treat the entered lowercase letter as the corresponding uppercase letter. The screen dialog might look like this:

 

Enter a single letter: C

The digit 2 corresponds to the letter C on the telephone. Enter a single letter: S

The digit 7 corresponds to the letter S on the telephone.

Enter a single letter: u

The digit 8 corresponds to the letter U on the telephone. Enter a single letter: L

The digit 5 corresponds to the letter L on the telephone. Enter a single letter: a

The digit 2 corresponds to the letter A on the telephone.

Enter a single letter: 9

Bye!

 

(Hints: use a loop. Also use either if-else if-else selections or switch statements in the loop. Refer to the program PickEmployee.java)

 

3. (Lewis & Loftus, PP5.8) Design and implement an application that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side by side. Print an appropriate statement if all three of the numbers are the same, or if any two of the numbers are the same. Continue playing until the user chooses to stop. Your output would be like

 

C:\cis283>java SimpleSlot

975

play again (y/n)? y

480

play again (y/n)? y

555

Jackpot!!!

play again (y/n)? y

622

Matched 2!!

play again (y/n)? n

C:\cis283>

 

 

(Hints: Use if-else inside a loop. Use nextInt() method of the Random class to generate three numbers individually.)

 

4. Write a program to ask the user to input an integer for the number of lines. Then use nested loops to print the following output (Assume the user entered 6.):

 

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

  • Like 1

Share this post


Link to post
Share on other sites

This is a version for the jackpot.

Just to give u an idea :)

 

This version works only for 3 numbers to generate..if u want to extend to 4 or more numbers

you have to think on different combination of matching...

 

If u want also a solution for every number with a complexity of O(n) let me know ;)

 

 


package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestClass{

   final static int numbersToGenerate = 3;

   public static void main(String[] args){

       String choice = null;


       BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

       // starting loop
       do{


           int[] generatedNumbers = getRandomNumbers();


           printNumbers(generatedNumbers);

           // see if at least 2 or 3 numbers are equals
           int numberofEquals = 1;



           for(int i=0; i < generatedNumbers.length; i++){
               int currNum = generatedNumbers[i];
               for(int j=i+1; j < generatedNumbers.length; j++){

                   // System.out.print("i=" +i + " j=" + j + " => " + generatedNumbers[j]+ " == " + currNum + "?");
                   if(generatedNumbers[j] == currNum){
                       numberofEquals++;
                       // System.out.print(" yes");
                   }else{
                       // System.out.print(" no");
                   }
                   // System.out.print("\n");
               }
           }

           if(numberofEquals == TestClass.numbersToGenerate -1 ){
               System.out.println("Matched 2!!");
           }else if(numberofEquals >= TestClass.numbersToGenerate){
               System.out.println("Jackpot!!!");
           }

               System.out.println("play again (y/n)?");




               try {
                   choice = br.readLine();
               } catch (IOException e) {
                   e.printStackTrace();
               }
               //System.out.println("Choice: " + choice);

       }while(choice != null && "y".equalsIgnoreCase(choice));


   }

   private static void printNumbers(int[] generatedNumbers) {
       System.out.print("Generated: " );
       for(int j=0; j < generatedNumbers.length; j++){
           System.out.print(generatedNumbers[j]);
       }

       System.out.print("\n");

   }


   public static int[] getRandomNumbers(){
       int[] numeri = new int[TestClass.numbersToGenerate];

       for(int i=0; i < TestClass.numbersToGenerate; i++){
           double randNum = Math.random() * 9;
           int intRand = (int)Math.ceil(randNum);
           numeri[i] = intRand;
       }


       return numeri;
   }

}

Share this post


Link to post
Share on other sites

and this for the last excercise:

 

public static void main(String [] args){

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       try {
           System.out.print("Put a value: ");
           String s = br.readLine();
           int val = Integer.parseInt(s);
           System.out.println();

           for (int j = val; j > 0; j--){
            for(int i = 1; i <= j; i++){
               System.out.print( i + " ");

            }
            System.out.println("");

           }



           br.close();
       } catch (IOException e) {
           e.printStackTrace();
       }


   }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×