JAVA Objective This assignment will give you practice with s
JAVA
Objective
This assignment will give you practice with static methods, parameters, and returns.
•
The goal of this project is to take existing Java code, and compress
it as much as possible by re-
organizing it into methods.
Your program:
Your program will consist of a single Java file, which you should call RefactoredShapeDrawer.java. (The word \"refactored\" is
a software engineering term for re-
organized code.) The program should work IDENTICALLY to
the attached
Java code for the
OriginalShapeDrawer
class, meaning that if the user enters the
same information into the
RefactoredShapeDrawer
and the
OriginalShapeDrawer
, the output should be identical.
The key requirement for the
RefactoredShapeDrawer
will be that it reorganizes the code into methods, to make the program shorter and easier to understand.
You should copy all of the code from
OriginalShapeDrawer
into
RefactoredShapeDrawer
(but make sure you change the line for the declaration of the class
name). You should then edit the code in
R
efactoredShapeDrawer
to reorganize it.
Here are the specific requirements for your program:
•
It must behave identically to the
OriginalShapeDrawer
. If it doesn\'t behave identically, you will lose between 5 and 10 points, depending on how different
your pr
ogram\'s output is from the original.
•
You should introduce a minimum of 4 new methods, not including the main method. Each method must be something that is used during
the execution of
the program.
•
At least two of your methods should have parameters that af
fect the behavior of the method.
•
At least two of your methods should have returns, and the return values must be used in some significant way by the code that calls the methods.
•
Your program should be clearly organized. Individual methods should have a cle
ar purpose, and it should be easy to see what the purpose is from their
name and/or comments describing the method. The behavior of the whole program should be clear from the sequence of method cal
ls in the main method.
Bonus:
Besides re-
organizing the code, an additional goal for the project is to reduce the overall length of the code.
The TA will
give the best “3” submissions (i.e.,
the
shortest code that meets all of the requirements
) 10 points extra.
Keep in mind that for t
his assignment, LENGTH = NUMBER OF SEMI
-COLONS (;) PLUS NUMBER OF ARITHMETIC OPERATORS (+, -
, *, /, %) IN YOUR CODE.
So comments don\'t count towards the length of the code (you should still put comments in your code!), method headers don\'t count (you shoul
d still add new methods
if they help organize the code!), and blank lines or lines that consist only of { or } don\'t count (so you should still inden
t your code in accordance with good style!).
Also, the class constants declared outside of any method wil
l not count towards the length of the program, so please keep those in. It\'s good programming practice
to use class constants instead of plain numbers in your program anyway.
Instructions:
•
Under
project
Lab09
you’ve created for Part 1, add a new Java main class
called
RefactoredShapeDrawer.java
•
Be sure to document your code
(add comments on top of your java file).
•
In the comments
add your name, date, course, homework number, and
statement of problem.
•
Once you are done, upload
RefactoredShapeDrawer.java
through Blackboard
under Lab 09 link
.
•
You do NOT need to email the
OriginalShapeDrawer
.java file
Solution
package sample;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class RefactoredShapeDrawer {
public static void main(String[] args) {
Input input = new Input();
input.getinput();
//
GUI gui = new GUI();
gui.getinput();
//
MoveAnimal ma = new MoveAnimal();
ma.moveAnimal();
FileIO fileIO = new FileIO();
fileIO.writeFile();
fileIO.readFile();
}
}
class Input{
void getinput()
{
Scanner scan = new Scanner(System.in);
System.out.println(\"enter two strings\");//program to take two input strings and concatenate
String S1= scan.nextLine();
String S2 = scan.nextLine();
scan.close();
System.out.println(S1 + S2);
}
}
class GUI{
void getinput(){
String s = JOptionPane.showInputDialog(null,\"Enter a String\");//same program using jOptionPane
String s1=s.substring(0,s.indexOf(\' \'));
String s2=s.substring(s.indexOf(\' \'));
s= \"ID => \" + s1 + \":\" + \"Name =>\" + s2;
JOptionPane.showMessageDialog(null, s);
}
}
abstract class Animal{
void move(){
System.out.println(\"Animal is moving\");
}
//void move1(){
//System.out.println(\"extra fit\");
//}
}
class Mammal extends Animal{
@Override
void move(){
System.out.println(\"mammal is walking\");
}
//@Override
//void move1(){
//System.out.println(\"extra fitting\");
//}
}
class Birds extends Animal{
@Override
void move(){
System.out.println(\"birds is flying\");
}
}
class MoveAnimal{
void moveAnimal(){
ArrayList<Animal> animal = new ArrayList<Animal>();//program to add to array list
animal.add(new Mammal());
animal.add(new Birds());
animal.add(new Mammal());
animal.add(new Birds());
int i =0;
while(i<4){
animal.get(i).move();
// if (i%2==0){
// animal.get(i).move1();
// }
// else
// {
// animal.get(i).move();
// }
i++;
}
}
}
class FileIO{
void writeFile(){
try{
FileWriter fw = new FileWriter(\"fileio.txt\");//program to read from a input file and write to a output file
fw.write(\"ID NAME Salary\");
fw.write(\"\ \ \");
fw.write(\"-- ---- ------\");
fw.write(\"\ \ \");
fw.write(\"1 john 10000\");
fw.write(\"\ \ \");
fw.write(\"2 sean 12000\");
fw.write(\"\ \ \");
fw.write(\"3 dheem 50000\");
fw.close();
}
catch(IOException e){
e.printStackTrace();
}
}
void readFile(){
try{
FileReader fr = new FileReader(\"fileio.txt\");
BufferedReader br = new BufferedReader(fr);
String s;
while((s=br.readLine())!=null)
System.out.println(s);
}
catch(IOException e){
e.printStackTrace();
}
}
}





