Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.
ID: 3763554 • Letter: C
Question
Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.java and change them to return all zeros in ZeroSum.java. Next, create a class called RealSum.java. This class, RealSum, will extend the ZeroSum class. Having all zeros for our sums isn't very useful. We will need to override the parent, or super class methods to produce real results. Create the necessary methods to override all of those in the ZeroSum.java. When you are done run your classes against DrivingSum.java. public class DrivingSum { public static void main(String[] args) { int x = 10; int y = 20; // x + y = 30....yay? ZeroSum zero= new ZeroSum(); RealSum real= new RealSum(); System.out.println("Calling ZeroSum " + zero.sum(x, y) ); System.out.println("Calling ZeroSum " + real.sum(x, y) ); } } How to continue the following, I got stuck Solution: OverloadingSum.java package com.chegg.nancy.solutions; public abstract class OverloadingSum { abstract public int sum(int x, int y); } ___________________________________________________________________ ZeroSum.java package.solutions; public class ZeroSum extends OverloadingSum { @Override public int sum(int x, int y) { return 0; } } ______________________________________________________________________________________ RealSum.java package.solutions; public class RealSum extends ZeroSum { @Override public int sum(int x, int y) { return x + y; } } _______________________________________________________________________________ DrivingSum.java package.solutions; public class DrivingSum { public static void main(String[] args) { int x = 10; int y = 20; // x + y = 30....yay? ZeroSum zero = new ZeroSum(); RealSum real = new RealSum(); System.out.println("Calling ZeroSum " + zero.sum(x, y)); System.out.println("Calling RealSum " + real.sum(x, y)); } }
Explanation / Answer
zerosum.java:
import java.io.*;
import java.util.*;
public class zerosum {
ArrayList<String>res = new ArrayList<String>();
int n;
public String[] solve(int n) {
this.n = n;
char[] equ = new char[2 * n - 1];
equ[0] = '1';
dfs(1, '+', 1, equ, 2);
Collections.sort(res);
return res.toArray(new String[0]);
}
private void dfs(int sum, char lastSign, int lastNum, char[] equ, int currentNum) {
if (currentNum > n) {
if (sum == 0) res.add(new String(equ));
return;
}
int signPos = 2 * (currentNum - 1) - 1;
int numPos = signPos + 1;
// +
equ[signPos] = '+';
equ[numPos] = (char)('0' + currentNum);
dfs(sum +currentNum, '+', currentNum, equ, currentNum + 1);
// -
equ[signPos] = '-';
equ[numPos] = (char)('0' + currentNum);
dfs(sum - currentNum, '-', currentNum, equ, currentNum + 1);
// ' '
equ[signPos] = ' ';
equ[numPos] = (char)('0' + currentNum);
int newNum = lastNum * 10 + currentNum;
if (lastSign == '+') {
dfs(sum - lastNum + newNum, lastSign, newNum, equ, currentNum + 1);
} else {
dfs(sum + lastNum - newNum, lastSign, newNum, equ, currentNum + 1);
}
}
public static void main(String[] args) throws IOException {
String problemName = "zerosum";
BufferedReader f = new BufferedReader(new FileReader(problemName + ".in"));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
String[] res = (new zerosum()).solve(n);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(problemName + ".out")));
for (String s : res) {
out.println(s);
}
out.close(); // close the output file
System.exit(0); // don't omit this!
}
}
######################################
Realsum.java:
public class RealSum
{protected boolean[] flags;
protected double[] sums;
public RealSum()
{
flags = new boolean[ 1 ];
sums = new double[ 1 ];
}
public RealSum( final int capacity ){
final int ldu = Util.ldu( capacity ) + 1;
flags = new boolean[ ldu ];
sums = new double[ ldu ];
}
final public double getSum()
{
double sum = 0;
for ( final double s : sums )
sum += s;
return sum;
}
final protected void expand( final double s )
{
final double[] oldSums = sums;
sums = new double[ oldSums.length + 1 ];
System.arraycopy( oldSums, 0, sums, 0, oldSums.length );
sums[ oldSums.length ] = s;
final boolean[] oldFlags = flags;
flags = new boolean[ sums.length ];
System.arraycopy( oldFlags, 0, flags, 0, oldFlags.length );
flags[ oldSums.length ] = true;
}
final public void add( final double a )
{
int i = 0;
double s = a;
try{
while ( flags[ i ] )
{
flags[ i ] = false;
s += sums[ i ];
sums[ i ] = 0.0;
++i;
}
flags[ i ] = true;
sums[ i ] = s;
return;
}
catch ( final IndexOutOfBoundsException e )
{
expand( s );
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.