Java2assignments.docx

Assignment 2

Assignment Content

1.

Top of Form

Resource:

· "Console/File Input and Output" text file

For this assignment, you will build on "starter" code to create a Java™ program that prompts the user for input, accepts user input, and produces both console and file output.

Copy the linked code to a JAVA file.

Add Java® code based on the comments inside the code.

Note: Refer to this week's Individual "Week Two Analyze Assignment" for model code you can adapt to meet this assignment's requirements.

Test, debug, and run your code using the NetBeans editor to make sure it meets the program requirements.

Save your JAVA file with a .txt extension.

* Program: Week 2 Code Assignment

* Purpose: Console/file input and output

* Programmer: YOUR NAME HERE

* Class: PRG/421r13, Java Programming II

* Instructor: YOUR INSTRUCTOR HERE

* Creation Date: TODAY'S DATE

* Comments: Fill in code

*

***********************************************************************/

package demo;

import java.util.Scanner; // scanner is a predefined class for taking inputs from user

public class Startercode

{

public static void main(String[] args)

{

// prompts and accepts user input

Scanner input= new Scanner(System.in);

double data;

System.out.println("Enter value ");

// accepts file input

String fileName = "input.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

// produces both console and file output

try { // coding block to output data to file

fileStdout = new PrintStream(new FileOutputStream("data.txt")); // output file name is data.txt

System.out.println("=============="); // output to console, numbers 0 through 9

fileStdout.println("" + i);

// ENTER YOUR CODE HERE. Enter code to end program successfully. Bottom of Form

Assignment 3

Assignment Content

1.

Top of Form

For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an ouput stream to a separate output text file.

Copy and paste the following Java™ code into a JAVA source file in NetBeans: 

import java.io.BufferedReader;

import java.io.BufferedWriter; 

public class Datasort { 

public static void main (String [] args) { 

File fin =     // input file

File fout =   // create an out file 

// Java FileInputStream class obtains input bytes from a file

FileInputStream fis = new FileInputStream(fin);     

// buffering characters so as to provide for the efficient reading of characters, arrays, and lines

BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 

// 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 

Collections.sort(al);   // sorted content to the output file

{

System.out.println(s);             

}

 // close the 2 files                       

}

Add code as indicated in the comments.

Note: Refer to this week's Individual assignment, "Week Three Analyze Assignment," and to Ch. 8, "IO," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide.

Run and debug your modified program in NetBeans until it satisfies the requirements described above.

Save your finalized JAVA file with a .txt extension

Week 3 analysis assignment referred to

/**********************************************************************

* Program: Datasort

* Purpose: Java code that sorts, extracts data and saves it to a collection

* Programmer: I am student

* Class: PRG/421r13, Java Programming II

* Instructor:

* Creation Date: 12/01/2017

*

* Comments:

* Extracts data from a file, sorts it, displays it onscreen, and saves it.

*

***********************************************************************/

// 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;