Results 1 to 6 of 6
- 04-01-2012, 07:29 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Double HELP but kinda easy.....please REAL PRO help me fig this out
Okay, I have two sets of code, one for a project I am doing and another for a potential entry level java job. I am having trouble (you'll understand when you see the code) in prob a couple areas. Mainly here are my questions...I have figured out how to make a set of classes and one MAIN. Now...ive watched tutorials but need someone to make this makes sense and here it is;
1.) What is the proper way and guidelines to call the classes into MAIN and have them operate? yes I researched and etc but I need someone to make it make sense and the ORDER in which to do it
2.) How does MAIN go from one class part to another to perform all the necessary steps in a row? Question example like...I want to first ask for a product choice, then move onto a quantity, then onto calculating the costs...how do I declare each one CORRECTLY in MAIN and also move from one to the other?
3.) Can i declare int or double quantity and product since they have no initial values except default (double product; or double product = 0.0; which is right?) in main or do them in their respective classes? Also, can I just declare them public and if so, in MAIN or their classes? Ex. in Main being public double product; or public double product =0.0;...?
This is the link I am missing in my head and programming. once I figure this out....its off to the races. PLEASE HELP.
Here is the Product coding followed by the java job programming....you'll see how both are missing the connection from classes to the main for execution... please note that I intentionally left the JAVA JOB code in separate main classes as I did this one several days ago...and the Product one just recently
import java.util.Scanner; //for user input
public class SalesCalculator { //class header
public static void main(String[] args) { //main
Scanner input = new Scanner(System.in);
SalesCalculator my_salesCalculator = new SalesCalculator();
Product my_product = new Product();
Quantity my_qauntity = new Quantity();
Calc my_calc = new Calc();
}
class Product {
public int product;
System.out.println("Which Product number would you like... 1,2,3, or 4\n");
System.out.println(" 1:Hat $10.00 2:Shirt $20.00 \n");
System.out.println(" 3:Pants $30.00 4: Jacket $40.00 \n");
product = input.nextInt();
if(product == 1){
product = 10.00;}
if(product == 2){
product = 20.00;}
if(product == 3){
product = 30.00;}
if(product == 4){
product = 40.00;}
if(product == 0) {
System.out.println("Please enter a number between 1-4");}
else(product >= 5) {
System.out.println("Please enter a number between 1-4");}
}
class Quantity {
public int quantity;
System.out.println("What Quantity would you like?");
quantity = input.nextInt();
if(quantity == 0){
System.out.println("Please enter a valid number");}
else {
System.out.println("Your Quantity is %d, (+quantity)");}
}
class Calc{
double iCost;
double salesTaxAmount = 0.0;
double totalSaleAmount = 0.0;
double salesTaxPercentage = 6.5;
iCost = product*quantity;
salesTaxAmount = iCost*salesTaxPercentage;
totalSaleAmount = iCost+salesTaxAmount;
System.out.printf("Your Product is %.2f and you Quantity is: %d\n", (product, quantity));
System.out.printf("Total for this order before tax is: $%.2f\n", (+iCost));
System.out.printf("Total Sales Tax is: $%.2f\n", (+salesTaxAmount));
System.out.printf("Total for entire order is: $%.2f\n", (+totalSaleAmount));
System.out.println("Have a Great Day and Come Again");
}
}
JAVA JOB - listed before class is the PROGRAM REQUIREMENT it is to satisfy...
Specify the path for a directory or file:
package fileDirectoryProgram; // when constructing a set of classes or for projects, it is best to make them into package(s)
/**
*
* @author MMA ANGEL and online collaboration (research)
* Still need some work and training but good start
* Here are the different codings and possible classes to make
* All constructive criticism welcomed. I am a sponge
*
*/
import java.io.File; //importing the utility from the bin folder usually
public class FileDirDesignation {
public static void main(String[] a) { //only one main will be needed for a package put together, necessary for a JAVA PROGRAM TO WORK and is the start
File myDir = new File("C:/jdk7.1.0_03/src/java/io"); //this is the one I have
System.out.println(myDir); //System.out.println(); is used for printing out a line of whatever is inbetween the ()
}
}
Constructing a file path:
package fileDirectoryProgram;
import java.io.File;
public class PathConstruct { //designates the class we are using
public static void main(String[] args) { //only needed if the only class in the program, no need to repeat in one program
String filePath = File.separator + "Java" + File.separator + "IO";
File file = new File(filePath); //creating new filePath
System.out.println(file.getPath()); //gets the Path()
}
}
Backing up files:
package fileDirectoryProgram;
import java.io.File;
public class fileBackup{
private static void fileBackup(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied and stored for you.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in this directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
switch(args.length){
case 0: System.out.println("File not Mentioned. Try again!");
System.exit(0);
case 1: System.out.println("Destination file not Mentioned. Try again!");
System.exit(0);
case 2: copyfile(args[0],args[1]);
System.exit(0);
default : System.out.println("Multiple files are not allowed!");
System.exit(0);
}
}
}
Original text or pattern which will be replaced if found in the files (as many times as it was found):
New text or pattern which will replace the original one if found in the file (as many times as it was found):
Using replaceFirst and replaceAll methods:
package fileDirectoryProgram; //we are assuming it is part of a package including all required in this job ap process test
import java.util.regex.Matcher; //necessary for Matcher utility that matches the string below
import java.util.regex.Pattern; // necessary for the Pattern utility
public class RegMatches //specified class whatever you name it within the package
{
private static String REGEX = "secret ultra black ops"; // this is the text that will be found and replaced as specified by replaceAll(REPLACE) below
private static String INPUT = "The government has secret ultra black ops units. "; //this is a sample of the text found including the text to change
private static String REPLACE = "special and necessary covert"; // the text that will be outputted as the replacement for the above stated
public static void main(String[] args) { //to start the program but not necessary if one class in a group of many in a package
Pattern p = Pattern.compile(REGEX); // get matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE); // can choose different but here "replaceALL will replace ALL occurences of this text
System.out.println(INPUT); //ourprints the result for this it is The government has special and necessary covert units.
}
}
Optionally, the class might also get and argument for a path to a file for outputting a list of which files were modified:
package fileDirectory;
import java.io.File;
import java.util.Arrays; //Arrays utility
import java.util.Comparator; //Comparator utility for comparing
public class checkModifiedFiles {
public static void main(String[] args) {
File dir = new File("C:\\whicheveryouchoose\\dir"); //put into specified file pathway
File [] files = dir.listFiles();
Arrays.sort(files, new Comparator(){
public int compare(Object o1, Object o2) { // allows for integars to be specified
return compare( (File)o1, (File)o2);
}
private int compare( File f1, File f2){
long result = f2.lastModified() - f1.lastModified(); // lastModified are found
if( result > 0 ){
return 1;
} else if( result < 0 ){
return -1;
} else {
return 0;
}
}
});
for(int i=0, length=Math.min(files.length, 10000); i<length; i++) {
System.out.println(files[i]); /**I couldn't figure out how to set to ALL modified files so decided the last 10000 or
*however many is chosen should suffice for this example.
*
*Prints out the result and in this case, the last 10000 modified files if that many exist
*
*/
}
}
}
- 04-01-2012, 08:12 PM #2
Re: Double HELP but kinda easy.....please REAL PRO help me fig this out
Look around the FAQs of the site and discover how to post code so that it retains its formatting.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-01-2012, 08:13 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Double HELP but kinda easy.....please REAL PRO help me fig this out
seriously....reply with something more and truly useful with relevance to the question itself.... or just write the formatting for me...
- 04-01-2012, 08:15 PM #4
Re: Double HELP but kinda easy.....please REAL PRO help me fig this out
If you're not willing to put in that much effort, do your seriously expect that some volunteer will slog his/her way through your unformatted code? Get real.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-01-2012, 08:25 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Double HELP but kinda easy.....please REAL PRO help me fig this out
if a newbie can understand the code...and others need that much help...wow...
thx for your input
- 04-01-2012, 08:37 PM #6
Re: Double HELP but kinda easy.....please REAL PRO help me fig this out
Most volunteers will ask you to post properly formatted code.
The more work and preparation you do with your post, the better chance is someone will help you.If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
help me out am kinda new to java and can't figure out these errors..
By fireman in forum New To JavaReplies: 3Last Post: 03-16-2012, 11:07 PM -
Kinda stuck... help?
By Samir4021 in forum New To JavaReplies: 9Last Post: 12-05-2011, 02:45 PM -
Simple triangle printer (kinda)
By 5myl in forum New To JavaReplies: 2Last Post: 08-30-2011, 03:29 AM -
need help, weird question kinda.
By carlos123 in forum New To JavaReplies: 6Last Post: 01-22-2008, 03:19 AM -
k this is my ultimate project. kinda
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 04:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks