Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

INTRODUCTION Your professor, likes to watch tw series. One of these shows caught

ID: 3598502 • Letter: I

Question

INTRODUCTION Your professor, likes to watch tw series. One of these shows caught his attention, the peeches seems more enticing to his ears than any other show he has ever watched before. For some time, he could not figure out the reason for that, until one day he realized that this show had various vocabulary. Acting scenes are full of long speeches, decent acting, and most importandy, certain vocabulary almost never gets repeated in the entire show. Your professor, being the nerd, he is, forms a theory. *A show ahat has the least repeated vocabulary should be ranked amon the best shows." The theory states that if story A and story B have the same number of words in them, and story A has less repeated words in it than that of story B, then story A is a better story. You are given 4 different stories as text documents of roushly the same size, 4600 words. In this assigament, your professor is asking you to build a tool that loads these stories and prints them in the order of their vocabulary ranks REQUIREMENTS You are given hws.java below. This class has a main function that invokes two other classes: StoryRanker and Story. You are required to write StoryRanker.java and Story-java, and submit thenm along with hws.java on blackboard. import jawa.ti1.Scanner: public class Hs public static void main(Stringtl args) throws FileNatFoundException( String userInput Scanter in nw Scanner(Systen.in) System.out.println("write a file name to include in your ranks. .?.. +"to list ranks, andI to xit progran: (Iuserinput. while quals(*'.)) { System.out.print ( userIngut in.next() if (userInput.equals(") System.out.printin( nGood Bye ) else if (userInput.equals) else f Story nyStory new Storytuserinput): if (yStery etordtount)e) StoryRanker.addstory(yStery):

Explanation / Answer

//File Name: Story.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

// Class Story definition

public class Story

{

// To store word count

int wordCount;

// To store file name

String title;

// Scanner class object for file reading

Scanner fileRead;

// File class object declared

File file;

// Parameterized constructor to receive file name

Story(String fileName)

{

// File class object initialized with file name passed to the constructor

file = new File(fileName);

// Extracts file name by extracting extension

title = fileExtension(fileName);

}// End of constructor

// Method to return number of words in the file

int getWordCount()

{

// Try block begin

try

{

// Read the file

fileRead = new Scanner(file);

// Check whether the file contains data or not

while (fileRead.hasNextLine())

{

// Read the word

fileRead.next();

// Increase the counter to number of words in the file

wordCount++;

}// End of while

// Close the file

fileRead.close();

} // End of try block

// Catch to handle file not found exception

catch (FileNotFoundException e)

{

System.out.println("File Not Fount");

}// End of catch

// Returns number of words in the file

return wordCount;

}// End of method

// Method to return only file name from the complete file name with extension

static String fileExtension (String str)

{

// Handle null case specially.

if (str == null) return null;

// Get position of last '.'.

int pos = str.lastIndexOf(".");

// If there wasn't any '.' just return the string as is.

if (pos == -1) return str;

// Otherwise return the string, up to the dot.

return str.substring(0, pos);

}// End of method

}// End of class

--------------------------------------------------------------------------------------------------------

//File Name: StoryRanker.java

// Class Data definition

class Data

{

// To store file name

String fileName;

// To store number of words

int wordcount;

}// End of class

// Class MyNewStack definition

class MyNewStack

{

// To store 100 file names and word count

// Data class array of objects created

Data myData[] = new Data[100];

// To point to top

int top;

// To store length

int len;

// Default constructor

MyNewStack()

{

top = -1;

len = 0;

}// End of constructor

// Returns false if top is -1 otherwise true

boolean isEmpty()

{

if(top == -1)

return false;

else

return true;

}// End of method

  

// Method to push an object to the stack

void push (Data d)

{

// Checks stack is full or not

if ( top == 99)

System.out.println("Stack is full.");

// Otherwise stack is not full

else

{

// Increase the top position of stack by one

++top;

// Instanitate the array top index position

myData[top] = new Data();

// Store s file name and number of words

myData[top].fileName = d.fileName;

myData[top].wordcount = d.wordcount;

// Increase the length by one

len++;

}// End of else

}// End of push

// Method to pop the stack top position which is an object

Data pop()

{

// Checks if stack top is less than zero stack is empty

if(top < 0)

{

System.out.println("Stack underflaow.");

return null;

}// End of if

// Otherwise

else

{

// Decrease the length by one

len--;

// Returns stack top position object

return myData[top--];

}// End of else

}// End of method

  

// Method to return the stack c position which is an object

Data peek(int c)

{

// Checks if stack top is less than zero stack is empty

if(top < 0)

{

System.out.println("Stack underflaow.");

return null;

}// End of if

// Otherwise

else

// Returns the stack top c position object

return myData[c];

}// End of method

} //End of class MyStack

---------------------------------------------------------------------------------------------------------------------------------------------------------

// Class StoryRanker definition

public class StoryRanker

{

// MyNewStack class two object created

static MyNewStack s1 = new MyNewStack();

static MyNewStack s2 = new MyNewStack();

// Data class object created

static Data d = new Data();

// Method to add file name and number of words to the stack

public static void addStory(Story myStory)

{

// Extracts data from myStory and stores it in the Data object

d.fileName = myStory.title;

d.wordcount = myStory.wordCount;

// Checks if stack is not empty

if(!s1.isEmpty())

// Push the object to the first stack

s1.push(d);

// Otherwise

else

{

// Loops till first stack top position  

for(int c = 0; c <= s1.top; c++)

{

// Checks if the stack word count is less than current file word count

if(s1.peek(c).wordcount < myStory.wordCount)

// Push the object to the second stack

s2.push(s1.pop());

}// End of for loop

// Push the current object to the first stack  

s1.push(d);

// Loops till second stack top position

for(int c = 0; c < s2.len; c++)

// Extracts second stack object and push it to the first stack

s1.push(s2.pop());

}// End of else

}// End of method

// Method to display first stack contents

// Displays file name and number of words

public static void printRanks()

{

// Loops till first stack top position

for(int c = 0; c <= s1.top; c++)

System.out.println((c + 1) + ": " + s1.peek(c).fileName + ", size = " + s1.peek(c).wordcount);

}// End of method

}// End of class

------------------------------------------------------------------------------------------------------------------------------------

//File Name: Hw5.java

import java.io.FileNotFoundException;

import java.util.Scanner;

// Class Hw5 definition

public class Hw5

{

// main method definition

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

{

// To store user entered data

String userInput = "";

// Scanner class object created

Scanner in = new Scanner(System.in);

System.out.println("Write a file name to include in your ranks. '?' to list rank, and ! to exit program");

// Accepts user choice and loops till user choice is not !

while(!userInput.equals("!"))

{

System.out.print(">> ");

userInput = in.next();

// Checks if use choice is "!" the stop

if(userInput.equals("!"))

System.out.println(" Good Bye :)");

// Checks if use choice is "?" the stop

else if(userInput.equals("?"))

// Calls the method printRanks() of StoryRanker class to display file name and number of words

StoryRanker.printRanks();

// Otherwise it is a file name

else

{

// Using default constructor pass the file name to Story class

Story myStory = new Story(userInput);

// Calls the method getWrodCount() of Story class to check if number of words is greater than zero

// then call addStory() method of StoryRanker class and pass the Story class object

if(myStory.getWordCount() > 0)

StoryRanker.addStory(myStory);

}// End of else

}// End of while

} // End of main method

}// End of class

File Test.txt contents

This is 'land' (sand) to write.

File Test1.txt contents

This is 'land' (sand) to write.
For.

File Test2.txt contents

This is.

File Data.txt contents

This is demo 4.
this Is 4 test.

Sample Run:

Write a file name to include in your ranks. '?' to list rank, and ! to exit program
>> Test.txt
>> ?
1: Test, size = 6
>> Data.txt
>> ?
1: Data, size = 8
2: Test, size = 6
>> Test1.txt
>> ?
1: Data, size = 8
2: Test1, size = 7
3: Test, size = 6
>> Test2.txt
>> ?
1: Data, size = 8
2: Test1, size = 7
3: Test, size = 6
4: Test2, size = 2
>> !

Good Bye :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote