Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I'm stuck

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I'm stuck

    my programs with difficulty are below:

    Program 20: Alphabet.java
    Write a program that will receive 3 words, print out the word that comes last alphabetically despite whether it is uppercase or lowercase. Do this 3 times.

    I put: Please tell me my error.
    import java.lang.String;
    import java.util.Scanner;
    public class Alphabet
    {
        public static void main(String[] args)
        {
        Scanner input=new Scanner(System.in);
        System.out.println("Please enter a word");
        String wordone=input.next();
        System.out.println("Please enter another word");
        String wordtwo=input.next();
        System.out.println("Please enter a third word");
        String wordthree=input.next();
        String wordfour=wordone.compareTo(wordtwo);
        String wordfive=wordtwo.compareTo(wordthree);
        String wordsix=wordone.compareTo(wordthree);
        System.out.println (wordfour);
        System.out.println (wordfive);
        System.out.println (wordsix);
     
       }
     
    }

    Program 21: Middle.java
    Write a program that will receive a personís full name in one String object (donít read in 3 names). Then receive a second personís full name. Print out whether the 2 people have the same middle name or not. Do this 3 times.


    I am unable to understand how to print out only the middle name.

    I put:
    import java.util.Scanner;
    public class Middle
    {
       public static void main(String[] args) 
       {
       Scanner input=new Scanner(System.in);
       System.out.println("Please enter your first name");
       String firstone=input.next();
       System.out.println("Please enter your middle name");
       String middleone=input.next();
       System.out.println ("Please enter your last name");
       String lastone=input.next();
       for(int i=0;i<length;i++)
       {
       System.out.println(middleone);
       }
     
      }
     
    }

    Program 22: WeCanWorkItOut.java
    Write a program that will receive 5 numbers and print out the largest number and the smallest number. Print the average.
    import java.util.Scanner;
    import java.util.Random;
    public class GuessWhat
    {
    	public static void main (String[] args) 
    	{
    	GuessWhat example= new GuessWhat();
    	example.getRandomNumber();
    	}
     
     
        public void getRandomNumber()
        {
        int min=50;
        int max=100;
        Random randGenerator= new Random();
        int randomNum=randGenerator.nextInt(max-min+1)+ min;
        System.out.println("Random number generated is:" + randomNum);
        }
     
    }

    PLEASE HELP ME WITH OTHER PROGRAMS ALSO BECAUSE MY DEADLINE IS TOMORROW.
    Sincerely,
    ian


  2. #2
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default im stuck!

    Program 22: WeCanWorkItOut.java
    Write a program that will receive 5 numbers and print out the largest number and the smallest number. Print the average.
    import java.util.Scanner;
    public class WeCanWorkItOut
    {
        public static void main(String[] args) 
        {    
        Scanner input=new Scanner(System.in);
        System.out.println("Please enter a number");
        double num1=input.nextDouble();
        System.out.println("Please enter a second number");
        double num2=input.nextDouble();
        System.out.println("Please enter a third number");
        double num3=input.nextDouble();
        System.out.println("Please enter a fourth number");
        double num4=input.nextDouble();
        System.out.println("Please enter a fifth and final number");
        double num5=input.nextDouble();
     
        if(num1<num2)
        if(num2<num3)
        if(num3<num4)
        if(num4<num5)
        {
        System.out.println("The first number entered is the smallest number");
        }
        else
        {
        System.out.println("Some of the numbers might be equal");
        }
     
        if(num1>num2)
        if(num2>num3)
        if(num3>num4)
        if(num4>num5)
        {
        System.out.println("The first number entered is the largest number");
        }
        else
        {
        System.out.println("Some of the numbers might be equal");
        }
     
        if(num1<=num2)
        if(num2<=num3)
        if(num3<=num4)
        if(num4<=num5)
        {
        System.out.println("There isn't a number with the lowest value as some or all of these numbers may be equal");
        }
        else
        {
        System.out.println("Try entering other numbers");
        }
     
        if(num1>=num2)
        if(num2>=num3)
        if(num3>=num4)
        if(num4>=num5)
        {
        System.out.println("There isn't a number with the highest value as some or all of the values may be equal");
        }
        else
        {
        System.out.println("Try entering other numbers");
        }
     
        double average;
        average= (num1+num2+num3+num4+num5)/5;
        System.out.println ("Average:"+average);
     
        }
     
    }
    Program 24 Shoot.java
    Program Rock-Paper-Scissors. The computer will randomly choose a rock, paper, or scissors. The user will input their choice. Tell the user who won and what was thrown. Do this 3 teams and give the overall win total.
    import java.util.Scanner;
    import java.util.Random;
     
    public class FinalShoot
    {
      public static void main(String[] args)
      {
        Scanner scan= new Scanner (System.in);
        String personPlay;    //User's play -- "R", "P", or "S"
        String computerPlay;  //Computer's play -- "R", "P", or "S"
        int computerInt;      //Randomly generated number used to determine
                              //computer's play
     
        System.out.println ("Enter your play: r, p, or s");
        personPlay = scan.nextLine();
        personPlay = personPlay.toUpperCase();
     
        System.out.println("You selected: " + personPlay);
     
        //an array that consists of R, P and S
        String[] computerPlayArray = {"R", "P","S"};
        Random generator = new Random();
        int computerSelected = generator.nextInt(computerPlayArray.length);
        //System.out.println ("From 1 to 3: " + computerPlay);
        //int play = generator.nextInt(3) + 1;
     
        //int computerPlay = computerInt;
        //int choice1=R;
        //int choice2=P;
        //int choice3=S;
        computerPlay = computerPlayArray[computerSelected];
        System.out.println ("computer Play:" + computerPlay);
     
        if (personPlay.equals(computerPlay)){
           System.out.println ("It's a tie!");
        }
        else if (personPlay.equals("R")){
            if (computerPlay.equals("S"))
               System.out.println ("Rock crushes scissors. You win!");
            else if (computerPlay.equals("P"))
               System.out.println ("Paper beats rock. Computer wins!");
        }
        else if (personPlay.equals("P")){
            if (computerPlay.equals("R"))
                System.out.println ("Paper beats rock. You win!");
            else if (computerPlay.equals("S"))
                System.out.println ("Scissors cut paper. Computer wins!");
        }
        else if (personPlay.equals("S")){
             if (computerPlay.equals("R"))
                 System.out.println ("Rock crushes scissors. Computer wins!");
             else if (computerPlay.equals("P"))
                 System.out.println ("Scissors cut paper. You win!");
        }
     
     
         }
    }

    Program 25 TwoFace.java
    The computer will generate a coin flip and print out if it was heads or tails. Do this 10 times and print out the results.
    import java.util.Random;
    public class TwoFace
    {
    	public static void main (String[] args)
    	{
    	TwoFace example= new TwoFace();
    	example.getHeadCount();
    	}
     
     
        public void getRandomItem();
        {
        	String[]coinSides={"head","tail"};
        	Random randgenerator= new Random();
        	int randomNum= randgenerator.nextInt(coinSides.length);
     
        }
     
        	for(int i=0;i<=10;i++)
    Last edited by helloworld922; November 5th, 2009 at 12:05 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: I'm stuck

    That's a lot for a single post, so I'm not going to go through all. But, for the first problem: read the API for String compareTo(Stirng s) method, it returns an int and not a string. Use these to compare equality and a series of conditionals to test the order. Alternatively (and this may help for the other problems as well) look at Arrays (Java 2 Platform SE v1.4.2), namely the sort method (hint read in values to an array)

Similar Threads

  1. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  2. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM
  3. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM