I have a task to write a program that takes in a profit score and multiplies it by 2, takes hard work score and multiplies it by 5, adds the two together, divides by 7 and then multiplies by 5000 to give a "bonus".
This would be very simple but the specification states i must use at least 9 methods not including main, use functions and getter/setter methods. The rest i can do but i don't know how to use ADT in a program like this.
This is what i've got so far...it does what its supposed to but its missing ADT.
import java.util.Scanner;
class bonus {
public static void main(String[] args) {
int i1 = inputProfit();
int w1 = inputWork();
int p1 = performanceScore(i1, w1);
int f1 = finalbonus(p1);
output(f1);
System.exit(0);
}//end main method
public static int inputProfit() {
Scanner scanner = new Scanner(System.in);
System.out.println("Profit score?");
int profitScore = scanner.nextInt();
return profitScore;
}
public static int inputWork() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("Hard work score?");
int workScore = scanner1.nextInt();
return workScore;
}
public static int profit(int profitScore) {
profitScore = profitScore*2;
return profitScore;
}
public static int work(int workScore) {
workScore = workScore*5;
return workScore;
}
public static int performanceScore(int profitScore, int workScore) {
int p = profit(profitScore);
int w = work(workScore);
int performance = ((p + w)/7);
return performance;
}
public static int finalbonus(int performance) {
int payOut = performance * 5000 ;
return payOut;
}
public static void output(int payOut) {
System.out.println("Your bonus is " + payOut + " pounds");
}
}//end class bonus
Related
I am writing a java program to compare two numbers using nesting methods but receiving the error`
class HelloWorld {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number");
int X = s.nextInt();
System.out.println("Enter Second Number");
int y = s.nextInt();
Nesting nest = new Nesting(int, int);
nest.disp();
}
}
class Nesting {
int m, n;
Nesting(int X, int y) {
m = X;
n = y;
}
int largest() {
if (m > n) {
return m;
} else {
return n;
}
}
void disp() {
int ans = largest();
System.out.println("My Result is " + ans);
}
}
While compiling receiving the following error
Line: 11
'.class' expected
Line: 11
'.class' expected
When you call a method or constructor, you should not pass the type, instead you have to pass the values, you have to change :
Nesting nest = new Nesting(int, int);
To this :
Nesting nest = new Nesting(X, y);
So i am trying to build a genetic algorithm on java i stuck on getting
fitness of my population here 3 classes from my project:
Class Individu
public class Individu {
int Popsize=4;
int Health[]= new int[Popsize];
int Attack[]= new int[Popsize];
int Atspeed[]= new int[Popsize];
int Move[]= new int[Popsize];
int health,attack,lifetime,dmgdone,attspeed,range,move;
double fitness;
double Pitness[]= new double[20];
Random random = new Random();
public int setHealth(){
health = random.nextInt(150 - 75) + 75;
return health;
}
public int setAttack(){
attack = random.nextInt(10 - 5) + 10;
return attack;
}
public int setAttspeed(){
attspeed = random.nextInt(3 - 1) + 3;
return attspeed;
}
public int setMoveSpeed(){
move = random.nextInt(8 - 4) + 1;
return move;
}
public int getGeneHealth(int index) {
return Health[index];
}
public int getGeneAttack(int index) {
return Attack[index];
}
public int getGeneAtspedd(int index) {
return Atspeed[index];
}
public int getGeneMove(int index) {
return Move[index];
}
public void setGene(int index, int value) {
Health[index]=value;
Attack[index]=value;
Atspeed[index]=value;
Move[index]=value;
fitness = 0;
}
public int size() {
return Popsize;
}
public double[] GenerateIndividual(){
for (int i = 0; i <Popsize; i++) {
Health[i]=setHealth();
Attack[i]=setAttack();
Atspeed[i]=setAttspeed();
Move[i]=setMoveSpeed();
}
return Pitness;
}
Class Fitness
public class Fitness {
Individu individu= new Individu();
double fitness;
double Pitness[]= new double[20];
public double getFitness(){
individu.GenerateIndividual();
for (int i = 0; i <=3; i++) {
fitness=
individu.getGeneHealth(i)+individu.getGeneAtspedd(i)+
individu.getGeneAttack(i)+
individu.getGeneMove(i));
fitness=fitness/171;
Pitness[i]=fitness;
System.out.println("Health from class
fitness"+individu.Health[i]);
}
return fitness;
}
}
Main Class
public class main {
public static void main(String[] args) {
Individu aaa=new Individu();
Fitness bbb= new Fitness();
bbb.getFitness();
aaa.GenerateIndividual();
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(3);
for (int i=0; i<=3; i++){
//System.out.println("Fitness ");
System.out.println("Generasi ke :"+i+1);
System.out.println("Health "+aaa.getGeneHealth(i));
System.out.println("Attackspeed "+aaa.getGeneAtspedd(i));
System.out.println("Attack "+aaa.getGeneAttack(i));
System.out.println("movementSpeed "+aaa.getGeneMove(i));
}
}
}
What i struggle is when i run this script i got 2 double value from 1 variable first value is from Fitness class as i printed here
System.out.println("Health from class fitness"+individu.Health[i]);
and second variable i printed here from Main Class
System.out.println("Health "+aaa.getGeneHealth(i));
that 2 variable is always have different value causing my fitness and my generation is not correlated each other.
My question is how to make this 2 variable print same value?
Well, aside from the many problems I can detect about the essentials of Genetic Algorithms, I see 'individu' and 'aaa' are two different Java objects.
Individu aaa=new Individu();
aaa.GenerateIndividual();
and
Individu individu= new Individu();
individu.GenerateIndividual();
Since your Health and Fitness are randomly generated on GenerateIndividual(), both 'aaa' and 'individu' will get different Health values.
I strongly recommend you to review GA essentials, since I can see many conception errors in your system.
I am trying to write code.It takes 3 input value 10 4 3.As you can see kdx value is 0.when it call function hello, value of qdx is changing to 11, But it should be 0.And return value is also 11.Do you have any idea why it is happening?
import java.util.Scanner;
import java.io.FileNotFoundException;
public class test1 {
private static final int FORWARD = 1;
private static final int BACKWARDS = -1;
private static Scanner scn = new Scanner(System.in);
public static void main(String args[]) throws FileNotFoundException {
int N = scn.nextInt();
int k = scn.nextInt();
int m = scn.nextInt();
while (N != 0) {
boolean[] offQueue = new boolean[N];
int offCount = 0;
int kdx = 0;
int mdx = N - 1;
kdx = hello(k, offQueue, kdx, FORWARD);
System.out.println(kdx);
}
}
private static int hello(int q, boolean[] offQueue, int qdx, int direction) {
return qdx;//Problem is here
}
}
I cannot reproduce your problem. Are you sure that you are using your last version when you run it? You can as well try to debug your application with Eclipse.
General info.
A mixed number is one of 3 forms:
1).An integer such as 12.
2).Fraction = int/int such as ¾
3.Mix of forms 1 and 2: 1 ¾ In this case one of more blanks act as a separator between integer and the fraction.
Question: 1.Treat the 1st and the 2nd forms as the special input and enhance the method parse to include these two cases.
* **I have already done the 3rd form of a mixed number and i am confused on how to parse the string to include an integer and a fraction alongside the mixed number in Mix.java . Output should be in gcd form. The following three programs run together.Some help me please thank you.
Here is my Mixed number code:
import java.util.Scanner;
class Mix extends Fraction{
public Mix(int n, int m) {super(n,m); }
public String displayMix() {
String str="";
if (first < second) str=first+"/"+second;
else str= first/second +" "+ first%second+"/"+second;
return str;
}//display
public Mix(String str) {
int[] iA= parse (str);
int top=iA[0]*iA[2]+iA[1];
int bot= iA[2];
int gcd = gcd(top,bot);
first=top/gcd;
second =bot/gcd;
}//Mix
public static Mix add (Mix s, Mix s2){
int gtop=s.first * s2.second
+ s2.first * s.second;
int gbottom= s.second * s2.second;
return (new Mix(gtop,gbottom));
}//add
public static String get (){
Scanner scan = new Scanner (System.in);
String userInput = scan.nextLine();
userInput =userInput.trim();
return (userInput);
} //get
public static int[] parse (String userInput){
int pos = userInput.indexOf(" ");
String sNum=userInput.substring(0,pos);
int iNum = Integer.parseInt(sNum);//first integer
String sNum2=userInput.substring(pos+1);
pos= sNum2.indexOf("/");
String sTop=sNum2.substring(0,pos);
int iTop = Integer.parseInt(sTop);//second integer
String sBot=sNum2.substring(pos+1);
int iBot = Integer.parseInt(sBot);//third integer
int[] sA = {iNum,iTop,iBot};
return (sA);
} //parse
public static void main(String[] args) {
System.out.print("Please enter mixed-format number :");
String userInput = Mix.get();
System.out.println("Input is: "+userInput);
Mix s = new Mix(userInput);
s.displayMix();
System.out.print("Please enter mixed-format number :");
userInput = Mix.get();
System.out.println("Input is: "+userInput);
Mix s2 = new Mix(userInput);
s2.displayMix();
Mix h= Mix.add(s,s2);
System.out.print(h.displayMix());
}//main
}//class
Here is the Fraction code:
public class Fraction extends Pair {
//attributes: NONE
public Fraction() { first=0; second=1;}
public Fraction(int n, int m) {
super(n,m);
int g=gcd(n,m);
first = first/g;
second=second/g;
}//Fraction
public String display2() {
String str = first+"/"+second;
return str;
}//display
public static Fraction add (Fraction f1, Fraction f2){
int gtop=f1.first * f2.second
+ f2.first * f1.second;
int gbottom= f1.second * f2.second;
return (new Fraction(gtop,gbottom));
}
public static int gcd (int n, int m){
while ( n!=m) {
if (n>m) n=n-m;
else m=m-n;
}//while
return (n);
}//gcd
//test the class
public static void main(String[] args) {
//pseudo-code is here
Fraction f= new Fraction();
f.display();
System.out.print(f.display2());
}//main
} //class
Finally here is pair:
import java.util.Arrays;
public class Pair {
int first;
int second;
public Pair (){first=0; second=0;}
public Pair(int n, int m) {
first=n;
second=m;
}//Pair
public int[] display() {
//pseudo_code is here
int[] c = {first, second};
return c;
}//display
public static void main(String[] args) {
//pseudo-code is here
Pair f= new Pair();
f.display();
System.out.println(Arrays.toString(f.display()));
}//main
} //class
You would have to make your own methods to do that using methods like str.split("/"), I don't know of any other way.
I've been working on this program for hours and I can't figure out how to get the program to actually print the grades from the scores Text file
public class Assign7{
private double finalScore;
private double private_quiz1;
private double private_quiz2;
private double private_midTerm;
private double private_final;
private final char grade;
public Assign7(double finalScore){
private_quiz1 = 1.25;
private_quiz2 = 1.25;
private_midTerm = 0.25;
private_final = 0.50;
if (finalScore >= 90) {
grade = 'A';
} else if (finalScore >= 80) {
grade = 'B';
} else if (finalScore >= 70) {
grade = 'C';
} else if (finalScore>= 60) {
grade = 'D';
} else {
grade = 'F';
}
}
public String toString(){
return finalScore+":"+private_quiz1+":"+private_quiz2+":"+private_midTerm+":"+private_final;
}
}
this code compiles as well as this one
import java.util.*;
import java.io.*;
public class Assign7Test{
public static void main(String[] args)throws Exception{
int q1,q2;
int m = 0;
int f = 0;
int Record ;
String name;
Scanner myIn = new Scanner( new File("scores.txt") );
System.out.println( myIn.nextLine() +" avg "+"letter");
while( myIn.hasNext() ){
name = myIn.next();
q1 = myIn.nextInt();
q2 = myIn.nextInt();
m = myIn.nextInt();
f = myIn.nextInt();
Record myR = new Record( name, q1,q2,m,f);
System.out.println(myR);
}
}
public static class Record {
public Record() {
}
public Record(String name, int q1, int q2, int m, int f)
{
}
}
}
once a compile the code i get this which dosent exactly compute the numbers I have in the scores.txt
Name quiz1 quiz2 midterm final avg letter
Assign7Test$Record#4bcc946b
Assign7Test$Record#642423
Exception in thread "main" java.until.InputMismatchException
at java.until.Scanner.throwFor(Unknown Source)
at java.until.Scanner.next(Unknown Source)
at java.until.Scanner.nextInt(Unknown Source)
at java.until.Scanner.nextInt(Unknown Source)
at Assign7Test.main(Assign7Test.java:25)
Exception aside, you actually are printing objects of type Record. What you would need to do is override toString() to provide a decent representation of your object.
#Override
public String toString() {
return "Something meaningful about your Record object.";
}
I also note that you're advancing the Scanner by use of nextLine() in System.out.println('...'). You may want to comment that part out of your code.
The reason you are getting this error is because of the fact that you are expecting an integer, but the next thing your scanner reads is not a number.
Also, put this in your toString of your record to stop printing out addresses.
i.e.
public static class Record {
public Record() {
}
public Record(String name, int q1, int q2, int m, int f)
{
}
public String toString(){}//print out stuff here.
}
change your Record to something like this
public static class Record {
String name;
int q1;
int q2;
int m;
int f;
public Record() {}
public Record(String name, int q1, int q2, int m, int f) {
// here you save the given arguments localy in the Record.
this.name = name;
this.q1 = q1;
this.q2 = q2;
this.m = m;
this.f = f;
}
#Override
public String toString(){
//here you write out the localy saves variables.
//this function is called when you write System.out.println(myRecordInstance);
System.out.println(name + ":" + q1 + ":" + q2 + ":" + m + ":" + f);
}
}
What it does: you have to save the argments by creating the Record.
Additional you have to override the toString method if you want to use System.out.println(myRecordInstance); instead you could write an other function returning a String in your Record and print out the return values of this function like System.out.println(myRecordInstace.writeMe()); Then you ne to add the function to the record.
public String writeMe(){
System.out.println(name + ":" + q1 + ":" + q2 + ":" + m + ":" + f);
}