Question

/* * CPS150_Lab10.java */ import java.io.*; import java.util.*; /** * CPS 150, Fall 2018 semester *...

/*
 * CPS150_Lab10.java
 */

import java.io.*;
import java.util.*;

/**
 * CPS 150, Fall 2018 semester
 *
 * Section N1
 * 
 * Lab Project 13: Comparing Java Strings
 *
 * @author *** Replace with your name ***
 */

public class CPS150_Lab13
{
  static final Scanner KBD = new Scanner(System.in);
  static final PrintStream OUT = System.out;
  
  // TO DO: Implement each of the following 4 methods,
  //        using the String compareTo method:
  
  /*
   * lessThan(String, String) -> boolean
   * 
   * method is passed two Strings
   * method returns true if the first String is less than the second String
   *  otherwise, the method returns false
   */
  
  /*
   * lessThanOrEquals(String, String) -> boolean
   * 
   * method is passed two Strings
   * method returns true if the first String is less than or equal to the second String
   *  otherwise, the method returns false
   */
  
  /*
   * greaterThan(String, String) -> boolean
   * 
   * method is passed two Strings
   * method returns true if the first String is greater than the second String
   *  otherwise, the method returns false
   */
  
  /*
   * greaterThanOrEquals(String, String) -> boolean
   * 
   * method is passed two Strings
   * method returns true if the first String is greater than or equal to the second String
   *  otherwise, the method returns false
   */
  
  public static void main(String[] args) throws FileNotFoundException
  {
    OUT.print("Enter the input file name: "); // prompt the user for an input file name
    String fileName = KBD.nextLine();
    Scanner fileIn = new Scanner( new File( fileName ) );

    String str1 = fileIn.nextLine(); // read three strings from the file
    String str2 = fileIn.nextLine();
    String str3 = fileIn.nextLine();
    
    // compare the strings lexicographically
    
    String middle = str1;   // Assume str1 is the middle string
    
    // TO DO: Complete the following code:
    //  1. using (calling) the methods implemented above
    //  2. nested branches that DO NOT use the boolean oprerators !, && or ||
    
    // TO DO: Check to see if str1 is <= str2 and str3 >= str2
    //        If so, set the middle String to str2
    
    // TO DO: Check to see if str3 is <= str2 and str1 >= str2
    //        If so, set the middle String to str2
    
    // TO DO: Check to see if str1 is <= str3 and str2 >= str3
    //        If so, set the middle String to str3
    
    // TO DO: Check to see if str2 is <= str3 and str1 >= str3
    //        If so, set the middle String to str3
    
    System.out.println("The middle string is " + middle); // output the middle-valued string
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:-

/*

* CPS150_Lab10.java

*/

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintStream;

import java.util.Scanner;

/**

* CPS 150, Fall 2018 semester

*

* Section N1

*

* Lab Project 13: Comparing Java Strings

*

* @author *** Replace with your name ***

*/

public class CPS150_Lab13 {

static final Scanner KBD = new Scanner(System.in);

static final PrintStream OUT = System.out;

// TO DO: Implement each of the following 4 methods,

// using the String compareTo method:

/*

* lessThan(String, String) -> boolean

*

* method is passed two Strings method returns true if the first String is less than the second String otherwise,

* the method returns false

*/

public boolean lessThan(final String str1, final String str2) {

final int var = str1.compareTo(str2);

if (var < 0) {

return true;

} else {

return false;

}

}

/*

* lessThanOrEquals(String, String) -> boolean

*

* method is passed two Strings method returns true if the first String is less than or equal to the second String

* otherwise, the method returns false

*/

public static boolean lessThanOrEquals(final String str1, final String str2) {

final int var = str1.compareTo(str2);

if (var <= 0) {

return true;

} else {

return false;

}

}

/*

* greaterThan(String, String) -> boolean

*

* method is passed two Strings method returns true if the first String is greater than the second String otherwise,

* the method returns false

*/

public boolean greaterThan(final String str1, final String str2) {

final int var = str1.compareTo(str2);

if (var > 0) {

return true;

} else {

return false;

}

}

/*

* greaterThanOrEquals(String, String) -> boolean

*

* method is passed two Strings method returns true if the first String is greater than or equal to the second

* String otherwise, the method returns false

*/

public static boolean greaterThanOrEquals(final String str1, final String str2) {

final int var = str1.compareTo(str2);

if (var >= 0) {

return true;

} else {

return false;

}

}

public static void main(final String[] args) throws FileNotFoundException {

OUT.print("Enter the input file name: "); // prompt the user for an input file name

final String fileName = KBD.nextLine();

final Scanner fileIn = new Scanner(new File(fileName));

final String str1 = fileIn.nextLine(); // read three strings from the file

final String str2 = fileIn.nextLine();

final String str3 = fileIn.nextLine();

// compare the strings lexicographically

String middle = str1; // Assume str1 is the middle string

// TO DO: Complete the following code:

// 1. using (calling) the methods implemented above

// 2. nested branches that DO NOT use the boolean oprerators !, && or ||

// TO DO: Check to see if str1 is <= str2 and str3 >= str2

// If so, set the middle String to str2

if (lessThanOrEquals(str1, str2)) {

if (greaterThanOrEquals(str3, str2)) {

middle = str2;

}

}

// TO DO: Check to see if str3 is <= str2 and str1 >= str2

// If so, set the middle String to str2

if (lessThanOrEquals(str3, str2)) {

if (greaterThanOrEquals(str1, str2)) {

middle = str2;

}

}

// TO DO: Check to see if str1 is <= str3 and str2 >= str3

// If so, set the middle String to str3

if (lessThanOrEquals(str1, str3)) {

if (greaterThanOrEquals(str2, str3)) {

middle = str3;

}

}

// TO DO: Check to see if str2 is <= str3 and str1 >= str3

// If so, set the middle String to str3

if (lessThanOrEquals(str2, str3)) {

if (greaterThanOrEquals(str1, str3)) {

middle = str3;

}

}

System.out.println("The middle string is " + middle); // output the middle-valued string

}

}

Text File:-

Deshmukh
Aditi
Anagha

OUTPUT:-

Enter the input file name: string.txt The middle string is Anagha . . Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
/* * CPS150_Lab10.java */ import java.io.*; import java.util.*; /** * CPS 150, Fall 2018 semester *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Can someone help me out with this? You are given a program that receives four lines...

    Can someone help me out with this? You are given a program that receives four lines in the below format, and stores them in str1, str2, str3, and num1. This is not a very long sentence. is long 4 Expand this program to: Write an if-elseif-else statement to print this line only if num1 is higher than 0: "Num1 is higher than 0!" print this line only if num1 is 0: "Num1 equals to 0!" And otherwise, print: ""Num1 is...

  • How to write a method in Java with these set of instructions: Method name will be:...

    How to write a method in Java with these set of instructions: Method name will be: public static boolean containsNumber(String[] array, String str) { // instruction: returns true if str is an element that is equal to an element of array // return false if str does not appear in array. // using compare String equality (str1.equals(str2). // JUST assume that array is not null and not empty // and NONE of strings in array is null. Assume str is...

  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can...

    package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can help decide if a particular programming project is best solved using a Waterfall or Agile methodology. Your program should ask the user: • How many programmers will be on the team [ More than 30 programmers -> Waterfall ] • If there needs to be firm deadlines and a fixed schedule [ Yes - > Waterfall ] • If the programmers have experience in...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB:...

    Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB: Step 0 - Getting Starting In this program, we have two classes, FileIO.java and FileReader.java. FileIO.java will be our “driver” program or the main program that will bring the file reading and analysis together. FileReader.java will create the methods we use in FileIO.java to simply read in our file. Main in FileIO For this lab, main() is provided for you in FileIO.java and contains...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT