Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - How can I make this more space efficient?
    primarykey
    data
    text
    <p>I've been asked to make this more space efficient, I'm assuming that I need to use loops but I'm not entirely sure how to, some help would be appreciated. </p> <p>(For reference the aim of this piece of code is to display a graph into another program using data which has already been collected in the other program)</p> <pre><code> public class StudentChart { public StudentChart(int[] moduleMarks) //Constructor { Bar y = new Bar(); y.makeVisible(); y.changeSize(1, 100); y.moveVertical(100); y.moveHorizontal(-1); y.changeColour(Colour.BLACK); //y-axis is produced Bar x = new Bar(); x.makeVisible(); x.changeSize(200,1); x.moveVertical(200); x.changeColour(Colour.BLACK); //x-axis is produced draw(moduleMarks); printSummary(moduleMarks); } public static void draw(int[] moduleMarks) { int a = moduleMarks[0]; int b = moduleMarks[1]; int c = moduleMarks[2]; int d = moduleMarks[3]; int e = moduleMarks[4]; int f = moduleMarks[5]; //stores module marks from array as variables to be used later Bar mod1 = new Bar(); Bar mod2 = new Bar(); Bar mod3 = new Bar(); Bar mod4 = new Bar(); Bar mod5 = new Bar(); Bar mod6 = new Bar(); mod1.makeVisible(); mod2.makeVisible(); mod3.makeVisible(); mod4.makeVisible(); mod5.makeVisible(); mod6.makeVisible(); //Bars are initialised and made visible mod1.moveVertical(200-a); mod2.moveVertical(200-b); mod3.moveVertical(200-c); mod4.moveVertical(200-d); mod5.moveVertical(200-e); mod6.moveVertical(200-f); //Bars are moved based on their height so that they touch the x-axis mod1.changeSize(15, a); mod2.changeSize(15, b); mod3.changeSize(15, c); mod4.changeSize(15, d); mod5.changeSize(15, e); mod6.changeSize(15, f); //Bar height changes depending on the module marks mod1.moveHorizontal(0); mod2.moveHorizontal(35); mod3.moveHorizontal(70); mod4.moveHorizontal(105); mod5.moveHorizontal(140); mod6.moveHorizontal(175); //Bars are moved across so can be seen on chart if (a&lt;35) { mod1.changeColour(Colour.RED); } if (a&gt;= 35 &amp;&amp; a&lt;40) { mod1.changeColour(Colour.YELLOW); } if (a&gt;= 40 &amp;&amp; a&lt;70) { mod1.changeColour(Colour.GREEN); } if (a&gt;= 70) { mod1.changeColour(Colour.MAGENTA); } if (b&lt;35) { mod2.changeColour(Colour.RED); } if (b&gt;= 35 &amp;&amp; a&lt;40) { mod2.changeColour(Colour.YELLOW); } if (b&gt;= 40 &amp;&amp; a&lt;70) { mod2.changeColour(Colour.GREEN); } if (b&gt;= 70) { mod2.changeColour(Colour.MAGENTA); } if (c&lt;35) { mod3.changeColour(Colour.RED); } if (c&gt;= 35 &amp;&amp; a&lt;40) { mod3.changeColour(Colour.YELLOW); } if (c&gt;= 40 &amp;&amp; a&lt;70) { mod3.changeColour(Colour.GREEN); } if (c&gt;= 70) { mod3.changeColour(Colour.MAGENTA); } if (d&lt;35) { mod4.changeColour(Colour.RED); } if (d&gt;= 35 &amp;&amp; a&lt;40) { mod4.changeColour(Colour.YELLOW); } if (d&gt;= 40 &amp;&amp; a&lt;70) { mod4.changeColour(Colour.GREEN); } if (d&gt;= 70) { mod4.changeColour(Colour.MAGENTA); } if (e&lt;35) { mod5.changeColour(Colour.RED); } if (e&gt;= 35 &amp;&amp; a&lt;40) { mod5.changeColour(Colour.YELLOW); } if (e&gt;= 40 &amp;&amp; a&lt;70) { mod5.changeColour(Colour.GREEN); } if (e&gt;= 70) { mod5.changeColour(Colour.MAGENTA); } if (f&lt;35) { mod6.changeColour(Colour.RED); } if (f&gt;= 35 &amp;&amp; a&lt;40) { mod6.changeColour(Colour.YELLOW); } if (f&gt;= 40 &amp;&amp; a&lt;70) { mod6.changeColour(Colour.GREEN); } if (f&gt;= 70) { mod6.changeColour(Colour.MAGENTA); } //Colour changes depending on module mark //Could be improved } public static void printSummary(int[] moduleMarks) { for(int i =0; i&lt;moduleMarks.length; i=i+1) { System.out.println("Module "+ (i+1) + " " + moduleMarks[i]); } //Prints module marks in a table } public static void main(String[] args) { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload