Welcome to AssignmentCache!

Java

Need Help in Java Assignment?
We can help you if you are having difficulty with your Java Assignment. Just email your Java Assignment at admin@assignmentcache.com.
We provide help for students all over the world in Java Assignment.

3 Item(s)

per page

Grid  List 

Set Descending Direction
  1. PRG 421 Week 1 Individual Analyze Assignment Analyzing a Java Program Containing Abstract and Derived Classes

    PRG 421 Week 1 Individual Analyze Assignment Analyzing a Java Program Containing Abstract and Derived Classes

    Regular Price: $6.00

    Special Price: $4.00

    PRG 421 Week 1 Individual Analyze Assignment Analyzing a Java Program Containing Abstract and Derived Classes

    "Analyzing a Java™ Program Containing Abstract and Derived Classes"
    The purpose of creating an abstract class is to model an abstract situation.
    Example:
    You work for a company that has different types of customers: domestic, international, business partners, individuals, and so on. It well may be useful for you to "abstract out" all the information that is common to all of your customers, such as name, customer number, order history, etc., but also keep track of the information that is specific to different classes of customer. For example, you may want to keep track of additional information for international customers so that you can handle exchange rates and customs-related activities, or you may want to keep track of additional tax-, company-, and department-related information for business customers.
    Modeling all these customers as one abstract class ("Customer") from which many specialized customer classes derive or inherit ("International Customer," "Business Customer," etc.) will allow you to define all of that information your customers have in common and put it in the "Customer" class, and when you derive your specialized customer classes from the abstract Customer class you will be able to reuse all of those abstract data/methods.This approach reduces the coding you have to do which, in turn, reduces the probability of errors you will make. It also allows you, as a programmer, to reduce the cost of producing and maintaining the program.
    In this assignment, you will analyze Java™ code that declares one abstract class and derives three concrete classes from that one abstract class. You will read through the code and predict the output of the program.
    Read through the linked Java™ code carefully.
    Predict the result of running the Java™ code. Write your prediction into a Microsoft® Word document, focusing specifically on what text you think will appear on the console after running the Java™ code.
    In the same Word document, answer the following question:
    Why would a programmer choose to define a method in an abstract class, such as the Animal constructor method or the getName() method in the linked code example, as opposed to defining a method as abstract, such as the makeSound() method in the linked example?

    Supporting Material: Week One Analyze Assignment Text File
    /**********************************************************************
    * Program: PRG/421 Week 1 Analyze Assignment
    * Purpose: Analyze the coding for an abstract class
    * and two derived classes, including overriding methods
    * Programmer: Iam A. Student
    * Class: PRG/421r13, Java Programming II
    * Instructor:
    * Creation Date: December 13, 2017
    *
    * Comments:
    * Notice that in the abstract Animal class shown here, one method is
    * concrete (the one that returns an animal's name) because all animals can
    * be presumed to have a name. But one method, makeSound(), is declared as
    * abstract, because each concrete animal must define/override the makeSound() method
    * for itself--there is no generic sound that all animals make.
    **********************************************************************/

    package mytest;

    // Animal is an abstract class because "animal" is conceptual
    // for our purposes. We can't declare an instance of the Animal class,
    // but we will be able to declare an instance of any concrete class
    // that derives from the Animal class.
    abstract class Animal {
    // All animals have a name, so store that info here in the superclass.
    // And make it private so that other programmers have to use the
    // getter method to access the name of an animal.

    private final String animalName;
    // One-argument constructor requires a name.
    public Animal(String aName) {
    animalName = aName;
    }

    // Return the name of the animal when requested to do so via this
    // getter method, getName().
    public String getName() {
    return animalName;
    }

    // Declare the makeSound() method abstract, as we have no way of knowing
    // what sound a generic animal would make (in other words, this
    // method MUST be defined differently for each type of animal,
    // so we will not define it here--we will just declare a placeholder
    // method in the animal superclass so that every class that derives from
    // this superclass will need to provide an override method
    // for makeSound()).
    public abstract String makeSound();
    };

    // Create a concrete subclass named "Dog" that inherits from Animal.
    // Because Dog is a concrete class, we can instantiate it.
    class Dog extends Animal {
    // This constructor passes the name of the dog to
    // the Animal superclass to deal with.
    public Dog(String nameOfDog) {
    super(nameOfDog);
    }

    // This method is Dog-specific.
    @Override
    public String makeSound() {
    return ("Woof");
    }
    }

    // Create a concrete subclass named "Cat" that inherits from Animal.
    // Because Cat is a concrete class, we can instantiate it.
    class Cat extends Animal {
    // This constructor passes the name of the cat on to the Animal
    // superclass to deal with.
    public Cat(String nameOfCat) {
    super(nameOfCat);
    }

    // This method is Cat-specific.
    @Override
    public String makeSound() {
    return ("Meow");
    }
    }

    class Bird extends Animal {
    // This constructor passes the name of the bird on to the Animal
    // superclass to deal with.
    public Bird (String nameOfBird) {
    super(nameOfBird);
    }

    // This method is Bird-specific.
    @Override
    public String makeSound() {
    return ("Squawk");
    }
    }

    public class MyTest {
    public static void main(String[] args) {
    // Create an instance of the Dog class, passing it the name "Spot."
    // The variable aDog that we create is of type Animal.
    Animal aDog = new Dog("Spot");
    // Create an instance of the Cat class, passing it the name "Fluffy."
    // The variable aCat that we create is of type Animal.
    Animal aCat = new Cat("Fluffy");
    // Create an instance of (instantiate) the Bird class.
    Animal aBird = new Bird("Tweety");
    //Exercise two different methods of the aDog instance:
    // 1) getName() (which was defined in the abstract Animal class)
    // 2) makeSound() (which was defined in the concrete Dog class)
    System.out.println("The dog named " + aDog.getName() + " will make this sound: " + aDog.makeSound());
    //Exercise two different methods of the aCat instance:
    // 1) getName() (which was defined in the abstract Animal class)
    // 2) makeSound() (which was defined in the concrete Cat class)
    System.out.println("The cat named " + aCat.getName() + " will make this sound: " + aCat.makeSound());
    System.out.println("The bird named " + aBird.getName() + " will make this sound: " + aBird.makeSound());
    }
    }

    Learn More
  2. PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

    PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

    Regular Price: $6.00

    Special Price: $4.00

    PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

    "Demonstrate the Coding to Produce Output to a File" text file
    For this assignment, you will analyze Java™ that presents instructional text on the console, accepts user input, and then creates a file based on that user input.
    Read the linked Java™ code carefully.
    Then, answer the following questions in a Microsoft® Word file:
    As you run the program in NetBeans the first time, at the prompt (the program will pause for input) type abc Return def Return ghi Ctrl+Shift+Del. What is the result?
    As you run the program in NetBeans the second time, at the prompt (the program will pause for input) type 123 Ctrl+Shift +Del. What is the result?
    What happens if the file Data.txt already exists when you run the program?
    What happens if the file Data.txt does not already exist when you run the program?

    Submit your Word file to the Assignment Files tab.

    /**********************************************************************
    *   Program:   FileOut 
    *    Purpose:    Demonstrate the coding to produce output to a file.  
    *   Programmer:   I am student         
    *   Class:       PRG/421r13, Java Programming II         
    *   Instructor:             
    *   Creation Date:   01/03/2018 
    *
    ***********************************************************************/
    package fileout;
    import java.io.*;

    public class FileOut {
    public static void main(String[] args) {

    InputStream istream;
    OutputStream ostream=null;
       // FileOutputStream creates an OutputStream that you can use to write bytes to a file.
    int c;                     // character stream 
    final int EOF = -1;                 // EOF indicator
    istream = System.in;

     
    // If the Data.txt file already exists, present its contents on the console.
    String fileName = "Data.txt";         // name of the file to open.
    String line = null;               // will reference one line at a time

    try {
    FileReader fileReader =        // FileReader reads text file
    new FileReader(fileName);         // reads in data from the file

    // always wrap FileReader in BufferedReader (to verify)
    BufferedReader bufferedReader =
    new BufferedReader(fileReader);

    System.out.println("Here are the contents of the current file named " + fileName + ":\n");
    while((line = bufferedReader.readLine()) != null) {
    System.out.println(line);        // verify / display what is read in by the program
    }  
    bufferedReader.close();         // close file

    }
    catch(FileNotFoundException ex) {       // coding to verify file can be opened
    System.out.println(           // if not open, error message to display
    "Unable to open file '" +
    fileName + "'");
    }
    catch(IOException ex) {           // exception, when there is an error in reading
    System.out.println(
    "Error reading file '"
    + fileName + "'");
    }

    // Now, let's construct a new file containing user input.
    System.out.println("\nType characters to write to file. After you finish, press Ctrl+Shift+Del to end.");
    File outFile = new File("Data.txt");    // create a new file

    try {                     // try block for EOF indicator
    ostream = new FileOutputStream(outFile);
    while ((c = istream.read()) != EOF)     // look for end of file in istream
    ostream.write(c);
    } catch (IOException e) {
    System.out.println("Error: " + e.getMessage());
    } finally {
    try {                     // try block for file error ñ file did not close

    istream.close();             // close input and output
    ostream.close();
    } catch (IOException e) {
    System.out.println("File did not close");
    }
    }
    }
    }

    Learn More
  3. PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

    PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

    Regular Price: $6.00

    Special Price: $4.00

    PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

    "Java Code That Sorts, Extracts Data and Saves It To a Collection" text file
    For this assignment, you will analyze code that uses a file input stream and a file output stream.
    Read through the linked Java™ code.
    In a Microsoft® Word document, answer the following questions:
    Could this program be run as is? If not, what is it lacking?
    Does this program modify the contents of an input stream? In what way?
    What are the results of running this code?
    Submit your completed Word document to the Assignment Files tab.
    Here is the code to answer the questions with:

    // import the needed classes
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;


    public class Datasort {

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

    File fin = new File("e:\\input.txt");   // input file on e: drive (with data)  
    File fout = new File("e:\\sorted.txt");   // create an out file on e: drive

    // Java FileInputStream class obtains input bytes from a file
    FileInputStream fis = new FileInputStream(fin); 
    FileOutputStream fos = new FileOutputStream(fout);

    // buffering characters so as to provide for the efficient reading of characters, arrays, and lines
    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));

    // declare an array in-line, ready for the sort

    String aLine;
    ArrayList<String> al = new ArrayList<String> ();

    int i = 0;
    while ((aLine = in.readLine()) != null) {
    // set the sort for values is greater than 0
       if (!aLine.trim().startsWith("-") && aLine.trim().length() > 0) {
           al.add(aLine);
           i++;
               }
           }

    Collections.sort(al);   // sorted content to the output file
    for (String s : al) {
    System.out.println(s);
       out.write(s);
       out.newLine();
       out.newLine();
       }
    // close the 2 files
           in.close();
           out.close();
       }
    }

    Learn More

3 Item(s)

per page

Grid  List 

Set Descending Direction