Question
need to do a framework as that picture ImagePresenter _ an applet that provides the graphical user interface_ a large area for displaying images,
need to do a framework as that picture " ImagePresenter" _ an applet that provides the graphical user interface_
a large area for displaying images, a smaller one for displaying text, and the four compass direction buttons that respond to user clicks.
then I need to use my the framework to implement a slide show, use the horizontal buttons to show images in the drawing area and texts in the text area. The texts should be memory pads for talking to the presentation.
The real framework classes are:
public abstract class Presenter extends java.applet.Applet implements ActionListener{
public abstract java.awt.Component
createCenterComponent(); #write code here public void showText(String text) {...}. #write code here
public abstract void northButtonPressed();
public abstract void eastButtonPressed();
public abstract void southButtonPressed();
public abstract void westButtonPressed(); ...
} public abstract class ImagePresenter
extends Presenter { public void showImage(String filename){...} #write code here public Component createCenterComponent() { // return a Canvas instance. #write code here // that can display images}}
I hope it's clear now! a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a "$integer", will replace that variable by a corresponding value in a 2ndfile, which will be called the "variable value file".
The requirements for the assignment:
1. The input and variable value file are both text files that will be specified in the command line. The way to run your program will thus be:
java cs401hw1.GenTextFile input_file variable_value_file
2. As indicated above, input_file is just a regular text file. Your code should not generate output based on the names of the files, of course.
3. Similarly, variable_value_file is also a regular text file, but each line of the file is considered to be the value of the next variable. That is, the first line of the file is the value of $0, the 2nd line is the value of $1, the 3rd line is the value of $2, and so on. The value does not include the end-of-line.
a. The java.util.Scanner class lets you easily read in the lines. Construct a File using the 2nd command-line argument, then pass this File to the constructor for Scanner
4. When a variable ($integer) is encountered in the input file, the corresponding line from the variable values file is displayed instead.
a. The java.io.FileReader class has a read() method that will read one character at a time
b. To convert the characters for integer into an actual integer value the static method Integer.parseInt(String) is helpful
5. A variable does not have to be used, or it may be used more than once.
6. Variable numbers are in the range of 0 to 9999.
7. Any two variables are separated by at least one character (so $1$2 won't be in the input)
8. Comment each function to explain what the function does, as well as the overall program.
9. Choose good variable and function names
10. Name the constants you use
Example:
$0 POPULATION GROWTH
(each * represents $1 person)
$2 $3
variable value file:
Town
1
1800
*
Expected output:
Town POPULATION GROWTH
(each * represents 1 person)
1800 *
Here is the program SOLUTION:::
package cs401hw1;
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner;
//Class: GenTextFile public class GenTextFile { private String inputFile; //instance variable inputFile private String varValFile; //instance variable varValFile //Constructor //Take values from input arguments and populate instance variables public GenTextFile(String[] args){ if(args!=null && args.length==2){ inputFile = args[0]; varValFile = args[1]; }else{ //if invalid number of arguments System.out.println("Invalid arguments!"); } } public static void main(String[] args){ GenTextFile gtf = new GenTextFile(args); //Create GenTextFile instance String currentLine = null; Scanner inFileScanner = gtf.createFileScanner(gtf.inputFile); //Create input file Scanner instance List variables = gtf.createVariables(gtf.varValFile); //Create List of variables from varValFile if(inFileScanner!=null){ while(inFileScanner.hasNextLine()){ //As long as there is a line in input file currentLine = inFileScanner.nextLine(); //read the line String updatedLineData = gtf.generateFinalDataForALine(currentLine, variables); //generate updted line data by replacing variables System.out.println(updatedLineData);//print the updated line in console } inFileScanner.close();//close input file scanner } } /** * Generate final data for currentLine by replacing variable * @param currentLine * @param variables * @return */ private String generateFinalDataForALine(String currentLine,List variables){ StringBuilder sb = new StringBuilder();//Create Stringbuilder char[] lineData = currentLine.toCharArray(); //Convert the currentLine String into CharArray for(int i = 0; i < lineData.length; i++){ //Traverse through individual characters //If current Character is '$' //Read next consecutive characters as long as they are digit if(lineData[i]=='$'){ String indexStr = ""; char currentChar; int p; for( p = i+1; p < lineData.length; p++){ //traverse from next characters currentChar = lineData[p]; if(!Character.isDigit(currentChar)){ //if it is not a digit break i = p-1; break; } indexStr +=currentChar; //concat digits into indexStr } int index = Integer.parseInt(indexStr); //convert indexStr into int index String variable = variables.get(index); //get variable from variables list at the index sb.append(variable); //append that variable in string builder if(p==lineData.length){//if the current digit is the last character of the line, breaks from outer for loop break; } }else{ //otherwise add the not digit character into the stringBuilder sb.append(lineData[i]); } } return sb.toString();//return string from the string builder } //Create file scanner from the given filename private Scanner createFileScanner(String fileName){ Scanner file = null; try { file = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println("Input file:"+fileName+" is not found!"); } return file; } /** * Create list of variables from the variable file name * @param varFileName * @return */ private List createVariables(String varFileName){ Scanner varFileScanner = createFileScanner(varFileName); //create file scanner List variables = null; if(varFileScanner!=null){ variables = new ArrayList(); while(varFileScanner.hasNextLine()){ variables.add(varFileScanner.nextLine());//add every line of file into the list } varFileScanner.close();//close var file scanner } return variables; } }
=====================================
THE FILES USED
=====================================
InputFile.txt
$0 POPULATION GROWTH (each * represents $1 person) $2 $3
VariableFile.txt
Town 1 1800 *
----So my question is this. Can someone exactly explain what is going on in this code solution to a beginner using comments for each line? Because I do not understand it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started