Download // // Assignment 4 // // File: Programmer.java // Author: Devin Dyer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
//
//
//
//
//
//
//
//
//
//
Assignment 4
File: Programmer.java
Author: Devin Dyer
Instructor: Dr. John Wang
Due: Due: February 22nd, 2016
Goal: Write a class that contains the salary
and writeOutput methods, which calculate
the users salary and create output when invoked.
public class Programmer
{
private int workingYears;
private String ssn;
private String name1;
public Programmer() {name1="New Programmer"; workingYears=0; ssn=
"000000000";}
public Programmer(String n, int y, String s)
{ name1=n; workingYears=y; ssn=s;}
public Programmer(String n, int y)
{ name1=n; workingYears=y;}
public Programmer(String n)
{ name1=n; }
public void set(String n, int y, String s)
{ name1=n; workingYears=y; ssn=s;}
public int salary()
{ return (42000 + 3000*workingYears); }
public void writeOutput()
{
System.out.println("\nName: " + name1);
System.out.println("\nYears worked: " + workingYears);
System.out.println("\nSSN: " + ssn);
System.out.println("\nYour salary: " + salary());
}
}
//
//
//
//
//
//
//
//
//
//
Assignment 4
File: ProgrammerDemo.java
Author: Devin Dyer
Instructor: Dr. John Wang
Due: February 22nd, 2016
Goal: Write a program that prompts the user
to input name, ssn and years worked
and invokes the writeOutput and salary method.
import java.util.Scanner;
class ProgrammerDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(true)
{
System.out.print("(Enter quit to exit) \n");
System.out.print("Enter your name: ");
String name1 = input.nextLine();
if(!name1.equals("quit"))
{
System.out.print("\nEnter total number
of years working: ");
int workingYears = input.nextInt();
System.out.print("\nEnter your SSN: ");
String ssn = input.next();
System.out.println("\n------------------------");
Programmer programmer = new Programmer();
programmer.set(name1, workingYears, ssn);
programmer.writeOutput();
System.out.println("\n------------------------");
}
else
{
System.out.println("\n\nDone\n\n\n");
System.exit(1);
}
}
}
}
//
//
//
//
//
//
//
//
//
//
//
Assignment 4
File: Sphere.java
Author: Devin Dyer
Instructor: Dr. John Wang
Due: February 22nd, 2016
Goal: Write a class that contains the area,
set and writeOutput methods, which stores
the spheres radius, computes area and
create output when invoked.
import java.text.DecimalFormat;
public class Sphere
{
DecimalFormat f1 = new DecimalFormat("0.00");
private double radius;
public Sphere() { radius = 0; }
public void set(double r)
{ radius = r; }
public double area()
{ return 4*Math.PI*radius*radius; }
public void writeOutput()
{
System.out.println("Area = " + f1.format(area()));
System.out.print("----------------------------\n");
}
}
//
//
//
//
//
//
//
//
//
//
Assignment 4
File: SphereDemo.java
Author: Devin Dyer
Instructor: Dr. John Wang
Due: February 22nd, 2016
Goal: Write a program that will prompt the user to
enter a sphere radius and will invoke the set, area
and output methods.
import java.util.Scanner;
class SphereDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Sphere one = new Sphere();
System.out.print("(Enter 0 to quit)\n");
System.out.print("Enter the radius of the circle: ");
double radius = input.nextDouble();
while(radius > 0)
{
one = new Sphere();
one.set(radius);
one.writeOutput();
System.out.print("(Enter 0 to quit)\n");
System.out.print("Enter the radius of the circle: ");
radius = input.nextDouble();
}
System.out.println("\n\nDone.\n\n");
}
}
Related documents