Question

In: Computer Science

Objective This assignment is to review what you should have learned in CSC 142 and learn...

Objective

This assignment is to review what you should have learned in CSC 142 and learn how I want you to do pseudocode and commenting for 143. Here’s the highlights this assignment will focus on:

  • Indefinite Loops

  • File Processing

  • Arrays

  • Classes

Description

You will choose a subject that’s familiar to you and create a program to read an inventory of items. You will create a java Class to hold the items, store them into an array of that class, and print a filtered set of them using a fencepost loop.

As an example, I created an inventory of fly fishing rods. Here’s a sample input file and console output:

Manufacturer Type Weight Length

Sage Standard 5 108

Sage Spey 9 132

TFO Standard 8 108

Orvis Standard 4 96

Which Field to filter on? Manufacturer

Enter the string: sage

Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]

Rod [Manufacturer=Sage, Type=Spey, Weight=9, Length=132]

Which Field to filter on? type

Enter the string: spey

Rod [Manufacturer=Sage, Type=Spey, Weight=9, Length=132]

Which Field to filter on? w

Enter the value: 5

Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]

Which Field to filter on? l

Enter the value: 108

Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]

Rod [Manufacturer=TFO, Type=Standard, Weight=8, Length=108]

Requirements

Your inventory must have the following:

  • At least 10 items

  • At least 4 fields with 2 different data types

  • The names of the fields should start with different letters to make it easier to code

Phase 1 - Pseudocode

You will write some brief pseudocode for your code so I can give you feedback your project before you go through writing the code.

Start by identifying a subject you’re familiar with which has a list of items you can put together. Then create the design for a Java Class to hold those items.  

See the “What is Pseudocode” page for information on how to write pseudocode and “Simple Guidelines” for what is correct pseudocode.

Phase 2 - Coding

You will write the complete code for your project and submit both the code, output, and debugging.

Code Requirements

  • Name the client class with main “Inventory” with matching case. This is extremely important or your code won’t run in my test environment.

  • Put all classes into one file by omitting the “public” from all but the first

  • Javadoc function headers with explanations of the function purpose, inputs, and returns

  • Comments above each code block, blank lines separating blocks that carry out one purpose, but none within the block

  • Do not comment each line, or lines which are obvious (e.g. print a blank line)

  • Data must be encapsulated as best as you can

  • Your main() should be a small as possible

  • All the real work should be in several very methods with clear purposes.

  • Ignores upper and lowercase

  • Accepts any input with the correct first letter in upper or lowercase

Solutions

Expert Solution

package file;

import java.io.*;

import java.util.Scanner;

/**

* Defines a class to store inventory information

*/

public class Inventory

{

/**

* Instance variable to store data

*/

String manufacturer;

String type;

int weight;

int length;

/**

* Default constructor to initialize default values to

* instance variables

*/

Inventory()

{

manufacturer = type = "";

weight = length = 0;

}// End of default constructor

/**

* Parameterized constructor to assign parameter values

* to instance variables

*/

Inventory(String ma, String ty, int we, int le)

{

manufacturer = ma;

type = ty;

weight = we;

length = le;

}// End of parameterized constructor

/*

* Overrides toString() method to return

* inventory information

* @see java.lang.Object#toString()

*/

public String toString()

{

return "Rod [Manufacturer = " + manufacturer +

", Type = " + type + ", Weight = " + weight +

", Length = " + length + "]";

}// End of method

/**

* Method to red file contents

* @param inv[] array of Inventory class object

* @return number of records

*/

int readFile(Inventory inv[])

{

// Local variable to store data read from file

String manufacturer;

String type;

int weight;

int length;

// Record counter

int counter = 0;

// Try block

try

{

// File Reader object created to read data

// from Popularity.txt file

File reader = new File("Inventory.txt");

// Scanner class object created

Scanner sc = new Scanner(reader);

// Loops till data available

while(sc.hasNext())

{

// Reads data

manufacturer = sc.next();

type = sc.next();

weight = sc.nextInt();

length = sc.nextInt();

/*

* Creates an object using

* parameterized constructor and assigns it

* at counter index position of array of object  

*/

inv[counter] = new Inventory(manufacturer, type,

weight, length);

// Increase counter by one

counter++;

}// End of while

// Closer the file

sc.close();

} // End of try block

// Catch block to handle file not found exception

catch(FileNotFoundException e)

{

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

e.printStackTrace();

}// End of catch

// Returns record counter

return counter;

}// End of method

/**

* Method to search data based on filter type

* @param inv[] - array of Inventory class object

* @param len - number of records

* @param filterType - Searching sting type

*/

void search(Inventory inv[], int len, String filterType)

{

// Scanner class object created

Scanner keyboard = new Scanner(System.in);

// Found status initially -1 for not found

int f = -1;

// To store the string data entered by the user to search

String strData;

// To store the integer data entered by the user to search

int intData;

// Checks the first character of the filterType

switch(filterType.charAt(0))

{

// Manufacturer

case 'M':

case 'm':

// Accepts manufacturer data

System.out.print("\n Enter the string: ");

strData = keyboard.next();

// Loops till number of records

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

/**

* Checks if current object manufacturer

* is equals to the user entered manufacturer

* sets the found status f to c value

* displays the found inventory record

*/

if(inv[c].manufacturer.

compareTo(strData) == 0)

{

f = c;

System.out.println(inv[c]);

}// End of if condition

break;

// Type

case 'T':

case 't':

// Accepts type data

System.out.print("\n Enter the string: ");

strData = keyboard.next();

// Loops till number of records

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

/**

* Checks if current object type is equals

* to the user entered type

* sets the found status f to c value

* displays the found inventory record

*/

if(inv[c].type.compareTo(strData) == 0)

{

f = c;

System.out.println(inv[c]);

}// End of if condition

break;

// Weight

case 'W':

case 'w':

// Accepts weight data

System.out.print("\n Enter the value: ");

intData = keyboard.nextInt();

// Loops till number of records

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

/**

* Checks if current object weight is equals

* to the user entered weight

* sets the found status f to c value

* displays the found inventory record

*/

if(inv[c].weight == intData)

{

f = c;

System.out.println(inv[c]);

}// End of if condition

break;

// Length

case 'L':

case 'l':

// Accepts length data

System.out.print("\n Enter the value: ");

intData = keyboard.nextInt();

// Loops till number of records

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

/**

* Checks if current object length is equals

* to the user entered length

* sets the found status f to c value

* displays the found inventory record

*/

if(inv[c].length == intData)

{

f = c;

System.out.println(inv[c]);

}// End of if condition

break;

case 'E':

case 'e':

System.exit(0);

default:

System.out.println("Invalid Field " +

"to filter");

}// End of switch case

// Checks if found status f value is -1 record not found

if(f == -1)

System.out.println("\n No such item.");

}// End of method

// main method definition

public static void main(String ss[])

{

// Scanner class object created

Scanner input = new Scanner(System.in);

// Creates an array of object of class Inventory

// of size 5

Inventory inv[] = new Inventory[5];

// Creates the first object using default constructor

inv[0] = new Inventory();

// Calls the method to read file and stores it in

// array of object

// Stores the return value as number of objects

int len = inv[0].readFile(inv);

// To store filter type entered by the user

String filterType;

// Loops till number of records and displays each record

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

System.out.println(inv[c]);

// Loops till user choice is not 'Exit' or 'E' or 'e'

do

{

// Accepts filter type from the user

System.out.print("\n Which Field to filter " +

"on (Exit to stop)? ");

filterType = input.next();

// Call the method to search record

// and displays it

inv[0].search(inv, len, filterType);

}while(true);// End of do - while loop

}// End of main method

}// End of class

Sample Output:

Rod [Manufacturer = Sage, Type = Standard, Weight = 5, Length = 108]
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length = 132]
Rod [Manufacturer = TFO, Type = Standard, Weight = 8, Length = 108]
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length = 96]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length = 132]

Which Field to filter on (Exit to stop)? Danger
Invalid Field to filter

No such item.

Which Field to filter on (Exit to stop)? Manufacturer

Enter the string: Orvis
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length = 96]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length = 132]

Which Field to filter on (Exit to stop)? Type

Enter the string: Sage

No such item.

Which Field to filter on (Exit to stop)? Type

Enter the string: Standard
Rod [Manufacturer = Sage, Type = Standard, Weight = 5, Length = 108]
Rod [Manufacturer = TFO, Type = Standard, Weight = 8, Length = 108]
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length = 96]

Which Field to filter on (Exit to stop)? Weight

Enter the value: 9
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length = 132]

Which Field to filter on (Exit to stop)? Length

Enter the value: 192

No such item.

Which Field to filter on (Exit to stop)? Length

Enter the value: 132
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length = 132]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length = 132]

Which Field to filter on (Exit to stop)? Exit

Inventory.txt file contents

Sage Standard 5 108
Sage Spey 9 132
TFO Standard 8 108
Orvis Standard 4 96
Orvis Spey 8 132


Related Solutions

Describe what you have learned in the course and what you are looking forward to learn...
Describe what you have learned in the course and what you are looking forward to learn in the future within the program?The program i pick is Allied health mananagement. Elements of assignment: 1-2 Pages (NOT including the cover or reference page) 12 Font, Times New Roman Double Spaced APA format Well-thought out commentary
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.        ...
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.         Task: In this assignment you will be doing the followings: 1. Write a program (either using Java, python, C, or your choice of programming language) to create three threads: one for deposit function and two for withdraw functions. 2. Compile and run the program without implementing any concurrency mechanism. Attach your output snapshot. 3. Now, modify your program to implement any concurrency mechanism. Compile...
What has been the thing you have learned and would have liked to learn about in...
What has been the thing you have learned and would have liked to learn about in regards to becoming a professional helper?
Write a paragraph to reflect on what you have learned in this assignment in terms of...
Write a paragraph to reflect on what you have learned in this assignment in terms of the difference between relational databases versus spreadsheets. Discuss ways in which a relational database could be useful for your work (if you have a job) or for your personal or professional projects (if you are a full-time student)
As you read the chapters and review the videos reflect on what you have learned about...
As you read the chapters and review the videos reflect on what you have learned about government intervention to redistribute income and assist with retirement and healthcare funding.. Write a two page presentation of your own views in your own words about the sources of inequality in wealth and income and whether this inequality should be reduced and how.
Review the three attributes you learned in Cybersecurity: Confidentiality, Integrity and Availability. Learn Information Assurance and...
Review the three attributes you learned in Cybersecurity: Confidentiality, Integrity and Availability. Learn Information Assurance and understand why two more attributes, Authentication and Nonrepudiation, should be involved in the Security Services dimension, and what the Time dimension for information security and assurance discusses. Write a short paper to discuss and describe your understanding.
Review the chapters and videos and reflect on what you have learned about the "sustainability" problems...
Review the chapters and videos and reflect on what you have learned about the "sustainability" problems with our economy and conventional economics. Write a two-page presentation of your own views in your own words about whether you agree that such problems exist, and if they do, whether the proposed solutions are adequate, appropriate, and sufficiently detailed and capable of being put into practice.
Overview In this assignment you will apply what you have learned about standard deviation and normal...
Overview In this assignment you will apply what you have learned about standard deviation and normal curves to solve a number of problems, including finding the area under selected sections of a normal curve using z-scores, then solve a problem using normal distributions. Part 1: The weights of all one hundred (100) 9th graders at a school are measured, and it is found that the mean of all the measurements is 100 lbs., with a standard deviation of 15 lbs.  ...
tax - corporate Review and reflect on what you have learned over the past 8 weeks....
tax - corporate Review and reflect on what you have learned over the past 8 weeks. Identify and discuss what the most practical and easily applied lesson you learned was. also, discuss which lesson was the hardest for you to grasp? why?
Community involvement assignment (1-2 pages) is for you to put what you have learned about in...
Community involvement assignment (1-2 pages) is for you to put what you have learned about in this current context to practice. Connect with at least (3) different people that relates to your professional goals (provide their names, background, and how you connected with them).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT