Java tutors in the USAJava is a computing language designed for the development of desktop and mobile applications, embedded systems, and the processing of big data. Introduced in 1995, Java is so much like C and C + + but easier to use because it enforces the object-oriented programming model. One ...
Data types in Java
class Artifact {
private String Name;
private int ArtifactID;
private int ArtifactYear;
private int AcquiredYear;
private double Value;
public Artifact() {
Name = "ARTIFACT";
ArtifactID = 0;
ArtifactYear = 0;
AcquiredYear = 0;
Value = 0;
}
public Artifact(String Name, int ArtifactID, int ArtifactYear, int AcquiredYear, double Value) {
setName(Name);
setArtifactID(ArtifactID);
setArtifactYear(ArtifactYear);
setAcquiredYear(AcquiredYear);
setValue(Value);
}
public void setName(String Name) {
this.Name = Name;
}
public void setArtifactID(int ArtifactID) {
this.ArtifactID = ArtifactID;
}
public void setArtifactYear(int ArtifactYear) {
this.ArtifactYear = ArtifactYear;
}
public void setAcquiredYear(int AcquiredYear) {
this.AcquiredYear = AcquiredYear;
}
public void setValue(double Value) {
this.Value = Value;
}
public String getName() {
return Name;
}
public int getArtifactID() {
return ArtifactID;
}
public int getArtifactYear() {
return ArtifactYear;
}
public int getAcquiredYear() {
return AcquiredYear;
}
public double getValue() {
return Value;
}
public String toString() {
return "*********************************************************\nName: " + Name + "\nArtifactID: " + ArtifactID + "\nArtifactYear: " + ArtifactYear + "\nAcquiredYear: " + AcquiredYear + "\nValue: " + Value+"\n";
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
public class Lab09MuseumArtifacts {
public static void main(String[] args) {
ArrayList< Artifact > artifacts = new ArrayList< Artifact > ();
try {
File ArtifactData = new File("D:\\My Stuff\\eclipse\\temp\\src\\temp\\ArtifactData.txt");
Scanner ArtifactDataReader = new Scanner(ArtifactData);
while (ArtifactDataReader.hasNextLine()) {
String data = ArtifactDataReader.nextLine();
String[] dataArray = data.split(" ");
artifacts.add(new Artifact(dataArray[0], Integer.parseInt(dataArray[1]), Integer.parseInt(dataArray[2]), Integer.parseInt(dataArray[3]), Double.parseDouble(dataArray[4])));
}
ArtifactDataReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
FileWriterArtifactMaster = new FileWriter("D:\\My Stuff\\eclipse\\temp\\src\\temp\\ArtifactMaster.txt");
FileWriterArtifactError = new FileWriter("D:\\My Stuff\\eclipse\\temp\\src\\temp\\ArtifactError.txt");
int errorRecordCount=0;
int validRecordCount=0;
for (int i = 0; i 2020) {
errors = errors + "Artifact Year must be less than 2020\n";
}
if (artifact.getAcquiredYear() <= 1899) {
errors = errors + "Acquired Year must be greater than 1899\n";
}
if (artifact.getAcquiredYear() 2020) {
errors = errors + "Acquired Year must be less than or equal to 2020\n";
}
if (artifact.getValue() < 0) {
errors = errors + "Artifact value must be greater than zero\n";
}
if(!errors.equals("")) {
if(errorRecordCount==0) {
ArtifactError.write(artifact.toString()+"##########ERRORS########\n"+errors);
}
else {
ArtifactError.append(artifact.toString()+"##########ERRORS########\n"+errors);
}
System.out.print(artifact.toString()+"##########ERRORS########\n"+errors);
errorRecordCount++;
}
else {
if(validRecordCount==0) {
ArtifactMaster.write(artifact.toString());
}
else {
ArtifactMaster.append(artifact.toString());
}
System.out.print(artifact.toString());
validRecordCount++;
}
}
System.out.println("\nGood Records: "+validRecordCount);
System.out.println("Records with errors: "+errorRecordCount);
ArtifactMaster.close();
ArtifactError.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Varuable Contolled While LOOP
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
// First of all, define the scanner
Scanner sc = new Scanner(System.in);
// Define helper variables
String userName = "";
int user_option;
int stop = 0; // this variable will tell the while-loop when to end
// In order to keep asking the user for the name (in case h/she wants to re-enter it), we have
// to use a while loop, that will be cancelled when the user enters "No"
while(stop == 0)
{
// User user for name
System.out.print("Please enter your name: ");
userName = sc.next();
// Ask if user wants to re-enter its name
int invalid_option = 1; // variable equals to 1 if the option entered is invalid
while(invalid_option == 1)
{
System.out.print(userName + " do you want to re-enter your name? Yes=1/No=0: ");
user_option = sc.nextInt();
if(user_option == 1) // user entered Yes
invalid_option = 0;
else if(user_option == 0) // user entered No
{
invalid_option = 0;
stop = 1; // stop the main loop
}
else
System.out.println("Invalid option. Please enter Yes=1/No=0");
}
}
// If user do not want to re-enter its name, the program terminates and the farewell message is displayed
System.out.println("Have a great day " + userName);
// close scanner
sc.close();
}
}
/*
*/