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.
Related
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
Array.java:
public class Array {
private double[][] recordGrade;
public Array(){
recordGrade = new double[5][2];
}
public double[][] getrecordGrade() {
return recordGrade;
}
public void setrecordGrade(double[][] recordGrade){
this.recordGrade = recordGrade;
}
ArrayTestDrive.java:
import java.util.Scanner;
import java.util.Arrays;
public class ArrayTestDrive {
#SuppressWarnings("resource")
public static void main(String[] args){
Array a = new Array();
Scanner s = new Scanner(System.in);
System.out.println("Input Grades from 0.0 to 100.00");
System.out.print("Enter grade at row[0] col[0]: ");
//a.getrecordGrade(a.setrecordGrade([0][0])) = a.getinput00() = s.nextDouble();
a.setrecordGrade([0][0]) = a.getinput00() = s.nextDouble(); //PROBLEM
Problem is I don't know how to write this code. I need to get the [0][1] from the other class. so that I can enter and write a value for [0][0].
Assign the input value to the row 0 and col 0 of the array directly. a.getrecordGrade() return you the reference to the array.
a.getrecordGrade()[0][0] = s.nextDouble();
Use methods with indexes as parameters:
public double getRecordGrade(int i, int y) {
return recordGrade[i][y];
}
public void setRecordGrade(int i, int y, double value){
this.recordGrade[i][y] = value;
}
Then call it with proper parameters:
double value = s.nextDouble();
a.setrecordGrade(0, 0, value);
I am having a String , i am creating a BIT of String based on the frequency of the element present
String:
abcdbcaab
Code:
class Test{
static int[][] dp;
public static void update(int i , int val ,int[] dpp){
while(i<=100000){
dpp[i]+=val;
i+= (i&-i);
}
}
public static int value(int i ,int[] dp){
int ans =0;
while(i>0){
ans+=dp[i];
i-= (i&i);
}
return ans;
}
public static void main(String args[] ) throws IOException {
Scanner in = new Scanner(new InputStreamReader(System.in));
dp = new int[27][1000001];
String s = in.next();
for(int i=0;i<s.length();i++){
update(i+1,1,dp[s.charAt(i)-'a']);
}
System.out.println(dp[0][7]); // Should show 2 as the frequency of 'a' at 7 position is 2
}
}
Where i am doing wrong . i could not get it but dp[0][8] is showing me 3Please Help i could not figure it out where i have commit mistake
Negativity should be avoided in life !!! but i think a little more negativity can improve you'r code:
value function you are doing wrong decrement of value of i
public static int value(int i ,int[] dp){
int ans =0;
while(i>0){
ans+=dp[i];
i-= (i&i); // Should be (i&-i);
}
return ans;
}
Here is the code:
import java.util.Scanner;
public class sending {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String text = giveMe(first);
System.out.println(text);
int x = scanner.nextInt();
x = number(x);
skrivUt(x);
}
//method for printing on screen
public static String giveMe(String first, String second){
first = ("Give me a number and I run down and add five to it");
second = ("Lol");
return first;
}
//method for doing math
public static int number(int x){
x = x + 5;
return x;
}
//method for printing out
public static void skrivUt(int x){
System.out.println(x);
}
}
As you can see I am new to this and I am having a problem with the main method and the method giveMe.
I want to have giveMe work as a collection of strings that I can call when I need them.
But when I try the above example I eclipse tells me that "first cannot be resolved to a variable" on line six String text = giveMe(first);
What am I doing wrong?
You are trying to use an enum and you never declared one... declare your enum like this outside your Main.
enum s {FIRST, SECOND} //add this
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String text = giveMe(s.FIRST); //add the s. so it knows to use your enum
System.out.println(text);
int x = scanner.nextInt();
x = number(x);
skrivUt(x);
}
Then you want to modify your method to take an enum instead like this
public static String giveMe(s string) {
switch (string) {
case FIRST:
return "Give me a number and I run down and add five to it";
case SECOND:
return "Lol";
}
return "invalid string";
}
Beginner, your problem is resolved.
Firstly declaration is important in java. "First" variable is not intailzed in your block of code. Ideally it is not necessary for your scenario.
Try this
import java.util.Scanner;
public class Test2 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String text = giveMe();
System.out.println(text);
int x = scanner.nextInt();
x = number(x);
skrivUt(x);
}
//method for printing on screen
public static String giveMe(){
String first = ("Give me a number and I run down and add five to it");
return first;
}
//method for doing math
public static int number(int x){
x = x + 5;
return x;
}
//method for printing out
public static void skrivUt(int x){
System.out.println(x);
}
}
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);
}