java - Passing an array between classes... stuck -
i have 3 java files working can't seem array print. have tried various things , seems going in circles. confused how work array different class. thank in advance help.
here main:
public class main { /** * @param args */ public static void main(string[] args) { testinteger ti = new testinteger(); testdouble td = new testdouble(); distance tdist = new distance(); mainmenu(); } public static void mainmenu() { int option; scanner sc = new scanner(system.in); while(true){ system.out.println("please choose type of numbers stored in array"); system.out.println(); system.out.println("******* main menu ********"); system.out.println("** 1. integers **"); system.out.println("** 2. double **"); system.out.println("** 3. distance **"); system.out.println("** 4. exit **"); system.out.println("**************************"); try{ option = sc.nextint(); switch (option) { case 1: { testinteger.arraymenu(); } case 2: { testdouble.arraymenu(); } case 3: { distance.arraymenu(); } case 4: { system.out.println("exiting program"); system.exit(0); } default: { system.out.println(); system.out.println("invalid option. please select option" + " 1 - 4"); system.out.println(); mainmenu(); } } }catch(inputmismatchexception e){ system.out.println("enter 1-4"); sc.nextline(); } } } }
the sortedarray class supposed hold sorted array of objects , methods use sortedarray.
import java.util.arrays; public class sortedarray implements comparable { //private int[] sa; //public sortedarray(int i, int j) { // todo auto-generated constructor stub //} public void constructor() { final int initialsize = 5; int incrementamount = 3; int top = -1; comparable [] sa = new comparable[initialsize]; //for(int i=0; i<5;++i){ // sortedarray sa = new sortedarray(); //} top = -1; //shows last item of array } public int appropriateposition(){ int ap = 0; return ap; } public int smallest(){ int smallest = 0; system.out.println("the smallest "); return smallest; } public int largest(){ int largest = 0; system.out.println("the largest "); return largest; } public void insert(int i){ int pos = 0; pos++; } public void find(){ } public void delete(){ } public comparable[] print(){ comparable[] sa = {2,3,4,5}; system.out.println(sa); return sa; } public void clear(){ system.out.println("now clearing array...................................."); // arrays.fill(sa, null); } public boolean full(){ boolean full = false; return full; } public boolean empty(){ boolean empty = false; return empty; } @override public int compareto(object arg0) { // todo auto-generated method stub return 0; }
the class testinteger inserting integers array , manipulating array in different ways.
import java.util.arrays; import java.util.inputmismatchexception; import java.util.scanner; public class testinteger implements comparable{ /** * user interface manipulating sorted array */ static sortedarray sa = new sortedarray (5,3); public static void arraymenu() { int option; while(true){ system.out.println(); system.out.println("**** integer array menu ****"); system.out.println("****************************"); system.out.println("** 1. insert **"); system.out.println("** 2. delete **"); system.out.println("** 3. clear **"); system.out.println("** 4. smallest **"); system.out.println("** 5. largest **"); system.out.println("** 6. return main menu **"); system.out.println("****************************"); scanner sc = new scanner(system.in); try{ option = sc.nextint(); switch (option){ case 1:{ try{ system.out.println("type integer insert: "); int x = sc.nextint(); int index = 0; sa.insert(x); sa.print(); }catch(inputmismatchexception e){ system.out.println("enter integers"); sc.nextline(); } arraymenu(); } case 2:{ try{ system.out.println("type index of item wish delete:"); int d = sc.nextint(); }catch(inputmismatchexception e){ system.out.println("enter integers"); sc.nextline(); } arraymenu(); } case 3:{ system.out.println("before clearing"); sa.print(); sa.clear(); system.out.println("after clearing"); sa.print(); arraymenu(); } case 4:{ sa.smallest(); arraymenu(); } case 5:{ sa.largest(); arraymenu(); } case 6:{ main.mainmenu(); } default: { system.out.println(); system.out.println("invalid option. please select option 1 - 6"); system.out.println(); arraymenu(); } } }catch(inputmismatchexception e){ system.out.println("enter 1-6"); sc.nextline(); } } } @override public int compareto(object o) { // todo auto-generated method stub return 0; } }
notice sortedarray class has no actual state. things in java exist inside braces enclose them. i'm assuming meant constructor be:
public void sortedarray(int initialsize, int incrementamount) { final int initialsize = 5; int incrementamount = 3; int top = -1; comparable [] sa = new comparable[initialsize]; top = -1; //shows last item of array }
constructors share name of class. code defaulting argument-less empty constructor.
but should more this:
private int incrementamount; private int top; private comparable[] sa; public void sortedarray(int initialsize, int incrementamount){ this.initialsize = initialsize; this.incrementamount = incrementamount; this.top = -1 this.sa = new comparable[initialsize]; }
think of classes sort of "blueprint" 'thing'. fields properties , data of 'thing'. constructor instantiation of 'thing', it's literally constructor cooks recipe class, according parameters.
Comments
Post a Comment