public enum TrafficLight {
RED(20),
YELLOW(3),
GREEN(10);
int duration;
TrafficLight(int newDuraction) {
duration = newDuraction;
}
public int getDuraction() {
return duration;
}
in the test class it's like:
public class TrafficLightTest {
public static void main(String[] args) {
for (TrafficLight trafficLight : TrafficLight.values()) {
System.out.println(trafficLight.name() + ": duraction " + trafficLight.getDuraction());
}
}
}
and i would like for example: "GREEN: duraction 10 " the number 10 to be like "**********".
10 = "**********" (ten asterisks)
3 = "***" (three asterisks)
i hope you get the idea. Thanks :)
Just use a for loop for printing "*" the required number of times
System.out.print(trafficLight.name() + ": duraction " ); //change this to Print instead of println
for(int i=0;i<trafficLight.getDuraction();i++)
System.out.print("*");
System.out.println();
Hope that helps.
You could do something like :-
public class TrafficLightTest {
public static String getAsteric(int input){ // return * using the int input
StringBuilder sb = new StringBuilder();
for(int i =0 ; i< input; i++)
sb.append("*");
return sb.toString();
}
public static void main(String[] args) {
for (TrafficLight trafficLight : TrafficLight.values()) {
System.out.println(trafficLight.name() + ": duraction " + TrafficLightTest.getAsteric(trafficLight.getDuraction())); // prints * equal to int input
}
}
}
Related
I am doing a social security program where you take apart a SS #, remove the hyphens (-), and parse the 3 parts into integers and add up.
Here is the main runner:
import static java.lang.System.*;
public class SocialRunner
{
public static void main( String args[] )
{
Social social = new Social("1-1-1");
//add test cases
//social.Social("1-1-1");
//social.chopAndAdd();
//boolean check = stringlivesmatter.checkEquality();
out.println(social);
}
}
And here is the main program:
import static java.lang.System.*;
public class Social
{
private String socialNum;
private String ssNum1, ssNum2, ssNum3, sub;
private int sum;
public Social()
{
}
public Social(String soc)
{
socialNum = soc;
}
public void setWord(String w)
{
/*String ssNum1 = socialNum.substring(0,socialNum.indexOf("-"));
String ssNum2 = socialNum.substring(socialNum.indexOf("-")+1,socialNum.indexOf("-"));
String ssNum3 = socialNum.substring(socialNum.indexOf("-")+1,socialNum.indexOf("-"));
*/
}
public void chopAndAdd()
{
sub = socialNum;
ssNum1 = socialNum.substring(0,socialNum.indexOf("-"));
ssNum2 = socialNum.substring(socialNum.indexOf("-")+1,socialNum.lastIndexOf("-"));
ssNum3 = socialNum.substring(socialNum.lastIndexOf("-")+1,0);
sum = Integer.parseInt(ssNum1) + Integer.parseInt(ssNum2) + Integer.parseInt(ssNum3);
}
public String toString()
{
sum = Integer.parseInt(ssNum1) + Integer.parseInt(ssNum2) + Integer.parseInt(ssNum3);
/*
String ssNum1 = socialNum.substring(0,socialNum.indexOf("-"));
String ssNum2 = socialNum.substring(socialNum.indexOf("-")+1,socialNum.indexOf("-"));
String ssNum3 = socialNum.substring(socialNum.indexOf("-")+1,socialNum.indexOf("-"));
sum = Integer.parseInt(ssNum1) + Integer.parseInt(ssNum2) + Integer.parseInt(ssNum3);
*/
return "SS# " + socialNum + " has a total of " + sum + "\n";
}
}
With the way the two programs above are written, I am given a runtime error:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at Social.toString(Social.java:46)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at SocialRunner.main(SocialRunner.java:20)
If I comment/delete:
sum = Integer.parseInt(ssNum1) + Integer.parseInt(ssNum2) + Integer.parseInt(ssNum3);
from
public String toString()
I get:
SS# 1-1-1 has a total of 0
How can I avoid a runtime or logic error?
You only need to remove the dashes to get your number.
public void chopAndAdd()
{
String con = socialNum.replaceAll("-", "");
sum = Integer.parseInt(ssNum1) ;
}
You probably may need to modify this as well
public String toString()
{
this.chopAndAdd();
return "SS# " + socialNum + " has a total of " + sum + "\n";
}
and
public static void main( String args[] )
{
Social social = new Social("1-1-1");
social.chopAndAdd();
out.println(social.toString(););
}
Take your time go through and analyse very well to your understanding
Change your constructor in Social as follows so that the ssNum variables are actually set.
public Social(String soc)
{
socialNum = soc;
String[] chunks = soc.split("-");
ssNum1 = chunks[0];
ssNum2 = chunks[1];
ssNum3 = chunks[2];
}
Using String.split will break up the ssn into a String[3], each element of which will contain the numbers excluding what you split on, in this case hyphens.
e.g. [123, 12, 1234]
Then all you need to do is parse them and sum them up.
public static void main(String[] args)
{
String socialString = "123-12-1234";
System.out.println(chopAndAdd(socialString));
}
public static int chopAndAdd(String s)
{
String[] chunks = s.split("-");
int first = Integer.parseInt(chunks[0]);
int second = Integer.parseInt(chunks[1]);
int third = Integer.parseInt(chunks[2]);
return first + second + third;
}
I am new to Java programming. I developed a Pizza class that takes for parameters and outputs the description and cost. I developed a PizzaOrderArray class that stores the pizza orders in an array. I have a class containing the main method also.
When I tried to print the values of the orders, nothing prints yet debugging shows that the proper methods and loops were entered.
What am I doing incorrect? I have invested many hours and am still very confused. Any suggestions, please? Thank you! I appreciate it.
Pizza.java
import java.text.NumberFormat;
import java.util.Locale;
public class Pizza {
public Pizza(String size, int numCheeseTop, int numPepTop, int numHamTop) {
if (!setPizzaSize(size)) {
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
setNumCheese(numCheeseTop);
setNumPep(numPepTop);
setNumHam(numHamTop);
}
public Pizza(String size, int numPepTop, int numHamTop) {
if (!setPizzaSize(size)) {
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_cheese = 0;
setNumPep(numPepTop);
setNumHam(numHamTop);
}
public Pizza(String size, int numHamTop) {
if (!setPizzaSize(size)) {
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_pep = 0;
setNumHam(numHamTop);
pizza_cheese = 0;
}
public Pizza(String size) {
if (!setPizzaSize(size)) {
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_cheese = 0;
pizza_pep = 0;
pizza_ham = 0;
}
public Pizza() {
pizza_size = "small";
pizza_cheese = 0;
pizza_pep = 0;
pizza_ham = 0;
}
public Pizza(Pizza copyPizza) {
pizza_size = copyPizza.getPizzaSize();
pizza_cheese = copyPizza.getNumCheese();
pizza_pep = copyPizza.getNumPep();
pizza_ham = copyPizza.getNumHam();
}
//Setters
public boolean setPizzaSize(String size) {
if (size.equalsIgnoreCase("small") || (size.equalsIgnoreCase("medium") || (size.equalsIgnoreCase("large")))) {
pizza_size = size.toLowerCase();
return true;
}
return false;
}
public void setNumCheese(int numCheeseTop) {
pizza_cheese = numCheeseTop;
}
public void setNumPep(int numPepTop) {
pizza_pep = numPepTop;
}
public void setNumHam(int numHamTop) {
pizza_ham = numHamTop;
}
//End of setters
//Getters
public String getPizzaSize() {
return pizza_size;
}
public int getNumCheese() {
return pizza_cheese;
}
public int getNumPep() {
return pizza_pep;
}
public int getNumHam() {
return pizza_ham;
}
//End of getters
public double calcCost() {
if (pizza_size.toLowerCase() == "small") {
return 10 + ((pizza_cheese + pizza_pep + pizza_ham) * 2);
}
if (pizza_size.toLowerCase() == "medium") {
return 12 + ((pizza_cheese + pizza_pep + pizza_ham) * 2);
}
if (pizza_size.toLowerCase() == "large") {
return 14 + ((pizza_cheese + pizza_pep + pizza_ham) * 2);
}
if (pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium"
&& pizza_size.toLowerCase() != "large") {
System.out.println("Invalid pizza size");
return 0;
}
return 0;
}
public String getDescription() {
return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
+ pizza_ham + " ham toppings "; //+ " which is " + money.format(pizza2.calcCost());
}
//private String pizza_size;
//private int pizza_cheese, pizza_pep, pizza_ham;
public String pizza_size;
public int pizza_cheese, pizza_pep, pizza_ham;
} //End of Pizza class
PizzaOrderArray.java
import static java.lang.System.out;
public class PizzaOrderArray {
public String pizza_size;
public int pizza_cheese, pizza_pep, pizza_ham;
//private String pizza_size;
//private int pizza_cheese; pizza_pep; pizza_ham;
private Pizza[] pizza;
private int index = 0;
public PizzaOrderArray() {
System.out.println("PizzaOrderArray()");
index = 1;
pizza = new Pizza[index];
}
public PizzaOrderArray(int i) {
System.out.println("PizzaOrderArray(int i)");
index = 1;
pizza = new Pizza[index];
}
public PizzaOrderArray(PizzaOrderArray poa) {
System.out.println("PizzaOrderArray(PizzaOrderArray poa)");
pizza = new Pizza[poa.index];
index = poa.index;
for (int i = 0; i < poa.index; i++) {
System.out.println("PizzaOrderArray(PizzaOrderArray poa) for loop");
pizza[i] = new Pizza(poa.pizza[i]);
}
}
public void setPizza(int index1, Pizza newpizza) {
System.out.println("Inside of setPizza");
pizza[index1] = new Pizza(newpizza);
}
public String getPizzaSize() {
System.out.println("Inside of getPizzaSize");
return pizza_size;
}
public int getNumCheese() {
System.out.println("Inside of getNumCheese");
return pizza_cheese;
}
public int getNumPep() {
System.out.println("Inside of getNumPep");
return pizza_pep;
}
public int getNumHam() {
System.out.println("Inside of getNumHam");
return pizza_ham;
}
public String toString() {
String s = "";
int indexUsed = 0;
System.out.println("Inside of toString");
for (int i = 0; i < indexUsed; i++) {
s = (s + pizza[i].toString());
}
System.out.println("Inside of toString for loop");
return s;
}
public double calcTotal() {
double r = 0.0;
System.out.println("Inside of calcTotal");
for (int i = 0; i < index; i++) {
System.out.println("Inside of calcTotal for loop");
r = r + pizza[i].calcCost();
}
return r;
}
public boolean equals(PizzaOrderArray orderarray) {
boolean r = false;
System.out.println("Inside of equals");
if (orderarray.pizza.length != pizza.length) {
System.out.println("Inside of equals if");
return r;
}
for (int i = 0; i < orderarray.pizza.length; i++) {
if (pizza[i].equals(orderarray.pizza[i])) {
System.out.println("Inside of equals for-if");
r = true;
} else {
System.out.println("Inside of equals for-else");
return false;
}
}
System.out.println("Return of equals");
return r;
}
} //End of PizzaOrderArray class
V4_Project_15_page_418.java
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.Arrays;
public class V4_Project_15_page_418 {
public static void main(String args[]) {
//Order1
PizzaOrderArray order1 = new PizzaOrderArray();
Pizza pizzaone = new Pizza("Medium", 0, 0, 0);
Pizza pizzatwo = new Pizza("Small", 1, 0, 0);
order1.setPizza(0, pizzaone);
System.out.println("Order 1: ");
System.out.println(order1.toString());
System.out.println(order1);
System.out.println();
//Order2
Pizza pizzathree = new Pizza(pizzatwo);
PizzaOrderArray order2 = new PizzaOrderArray(2);
order2.setPizza(0, pizzaone);
order2.setPizza(0, pizzatwo);
System.out.println("Order 2: ");
System.out.println(order2.toString());
System.out.println(order2);
System.out.println();
//Order3
PizzaOrderArray order3 = new PizzaOrderArray(1);
order3.setPizza(0, pizzaone);
order3.setPizza(0, pizzatwo);
System.out.println("Order 3: ");
System.out.println(order3.toString());
System.out.println(order3);
System.out.println();
//Order4
PizzaOrderArray order4 = new PizzaOrderArray(order3);
System.out.println("Order 4: ");
System.out.println(order4.toString());
System.out.println(order4);
//TEST THE PROGRAM
System.out.println("TEST: The total for order 4 is: " + order4.calcTotal());
System.out.println();
//Order5
PizzaOrderArray order5 = new PizzaOrderArray(order1);
System.out.println("Order5: ");
System.out.println(order5);
System.out.println();
}//End of main class
}//End of V4_Project_15_page_418 class
Output:
PizzaOrderArray()
Inside of setPizza
Order 1:
Inside of toString
Inside of toString for loop
Inside of toString
Inside of toString for loop
PizzaOrderArray(int i)
Inside of setPizza
Inside of setPizza
Order 2:
Inside of toString
Inside of toString for loop
Inside of toString
Inside of toString for loop
PizzaOrderArray(int i)
Inside of setPizza
Inside of setPizza
Order 3:
Inside of toString
Inside of toString for loop
Inside of toString
Inside of toString for loop
PizzaOrderArray(PizzaOrderArray poa)
PizzaOrderArray(PizzaOrderArray poa) for loop
Order 4:
Inside of toString
Inside of toString for loop
Inside of toString
Inside of toString for loop
Inside of calcTotal
Inside of calcTotal for loop
Invalid pizza size
TEST: The total for order 4 is: 0.0
PizzaOrderArray(PizzaOrderArray poa)
PizzaOrderArray(PizzaOrderArray poa) for loop
Order5:
Inside of toString
Inside of toString for loop
Take a close look at the condition in this for loop, it isn't going to ever print anything since the condition is never true since i is never less than indexUsed which is 0.
public String toString() {
String s = "";
int indexUsed = 0;
System.out.println("Inside of toString");
for(int i = 0; i < indexUsed; i++)
s= (s + pizza[i].toString());
System.out.println("Inside of toString for loop");
return s;
}
Something also need pay attention to:
for(int i = 0; i < indexUsed; i++)
s= (s + pizza[i].toString());
System.out.println("Inside of toString for loop");
means:
for(int i = 0; i < indexUsed; i++) {
s= (s + pizza[i].toString());
}
System.out.println("Inside of toString for loop");
So this is System.out.println just misleading you, you are never "inside of" the for loop.
I think it's better to always use the braces '{}' with for/while loop.
The snippet
int indexUsed = 0;
System.out.println("Inside of toString");
for(int i = 0; i < indexUsed; i++)
s= (s + pizza[i].toString()); is wrong, your for loop is never executed since indexUsed is 0
In your toString method, the for loop condition never becomes true (before the first iteration itself, 0<0 becomes false & loop terminates without executing once) so the loop never executes.
You can try changing the for loop statement to:
for(int i = 0; i < index; i++)
Can someone see why the user can enter more than 27 apple, blueberry, or peanut pies? Even after declaring a final int for the max number of each type of pie.
The object here is to continually prompt the user for type of pie until the user wants to quit. Each time one of the valid inputs is entered it is stored in it's own array. After the user has indicated they are finished, calculations are done and a message is printed.
import javax.swing.JOptionPane;
public class CalcPieProfit {
public static void main(String[] args) {
final int MAX_PER_TYPE = 27;
int appleTotal = 0;
int blueberryTotal = 0;
int peanutTotal = 0;
String typeOfPie = getPieType();
while (!typeOfPie.equalsIgnoreCase("q")) {
if (typeOfPie.equalsIgnoreCase("apple")) {
String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE);
appleTotal++;
}
else if (typeOfPie.equalsIgnoreCase("blueberry")) {
String[] blueberryArray = fillBlueberry(typeOfPie, MAX_PER_TYPE);
blueberryTotal++;
}
else if (typeOfPie.equalsIgnoreCase("peanut")) {
String[] peanutArray = fillPeanut(typeOfPie, MAX_PER_TYPE);
peanutTotal++;
}
typeOfPie = getPieType();
}
if (typeOfPie.equalsIgnoreCase("q")) {
int totalPies = calcTotalPies(appleTotal, blueberryTotal, peanutTotal);
double profit = calcProfit(appleTotal, blueberryTotal, peanutTotal);
printReport(totalPies, appleTotal, blueberryTotal, peanutTotal, profit);
}
}
public static String getPieType() {
String pieType;
do {
try {
pieType = JOptionPane.showInputDialog("Enter a pie type:");
}
catch (NumberFormatException e) {
pieType = "";
}
if (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") &&
!pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q")) {
JOptionPane.showMessageDialog(null, "Enter 'apple', 'blueberry', 'peanut', or 'q' only.");
}
} while (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") &&
!pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q"));
return pieType;
}
public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) {
String[] appleArray = new String[MAX_PER_TYPE];
for (int i = 0; i < appleArray.length; i++) {
appleArray[i] = typeOfPie;
}
return appleArray;
}
public static String[] fillBlueberry(String typeOfPie, int MAX_PER_TYPE) {
String[] blueberryArray = new String[MAX_PER_TYPE];
for (int i = 0; i < blueberryArray.length; i++) {
blueberryArray[i] = typeOfPie;
}
return blueberryArray;
}
public static String[] fillPeanut(String typeOfPie, int MAX_PER_TYPE) {
String[] peanutArray = new String[MAX_PER_TYPE];
for (int i = 0; i < peanutArray.length; i++) {
peanutArray[i] = typeOfPie;
}
return peanutArray;
}
public static int calcTotalPies(int appleTotal, int blueberryTotal, int peanutTotal) {
int total = appleTotal + blueberryTotal + peanutTotal;
return total;
}
public static double calcProfit (int appleTotal, int blueberryTotal, int peanutTotal) {
final double APPLE_PROFIT = 5.94;
final double BLUEBERRY_PROFIT = 5.89;
final double PEANUT_PROFIT = 6.95;
double profit = (APPLE_PROFIT * appleTotal) + (BLUEBERRY_PROFIT * blueberryTotal) +
(PEANUT_PROFIT * peanutTotal);
return profit;
}
public static void printReport(int totalPies, int appleTotal, int blueberryTotal, int peanutTotal, double profit) {
if (totalPies > 0) {
JOptionPane.showMessageDialog(null,
"Pie Report\n\n" +
"Total pies: " + totalPies +
"\nTotal of apple pie: " + appleTotal +
"\nTotal of blueberry pie: " + blueberryTotal +
"\nTotal of peanut butter pie: " + peanutTotal +
"\nTotal profit: $" + String.format("%.2f", profit));
}
else {
JOptionPane.showMessageDialog(null, "Enjoy your day off.");
}
}
}
You are not really using the String[]s appleArray, blueberryArray and peanutArray - they are created in their respective method but not used anywhere else. For calculating the profits, you are (rightfully) only the total variables.
Instead of
if (typeOfPie.equalsIgnoreCase("apple")) {
String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE);
appleTotal++;
}
you should do something like
if (typeOfPie.equalsIgnoreCase("apple")) {
if (appleTotal >= MAX_PER_TYPE) {
JOptionPane.showMessageDialog(null, "Too many apples.");
} else {
appleTotal++;
}
}
(and the same for other pie types).
You're redeclaring the pie arrays each time you go to add them.
public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) {
String[] appleArray = new String[MAX_PER_TYPE];
for (int i = 0; i < appleArray.length; i++) {
appleArray[i] = typeOfPie;
}
return appleArray;
}
Each time you call this method, a new "appleArray" is generated. If you want it to persist between calls to this method, declare the appleArray as private static outside of the loop, and reference that instead.
I have searched and found a few responses that didn't seem to help and I am stuck with an nullPointerException error. Below is my code the error is in my logResponse() method, any help is much appreciated.
import java.util.*;
public class Survey21 {
private Scanner in = new Scanner(System.in);
private String surveyTitle;
private static int rID;
private static int respondentID;
private int[][] responses;
//Constructors
// Default Constructor
Survey21() {
surveyTitle = "Customer Survey";
}
// Overloaded Constructor 1
Survey21(String title, int rID) {
surveyTitle = title;
rID = 0;
generateRespondentId();
}
Survey21(int[][] surveyArray) {
responses = surveyArray; // store responses
}
public static int getrID() {
return rID;
}
public static void setRespondentID(int respondentID) {
respondentID = rID;
}
public String getTitle() {
return surveyTitle;
}
public void setTitle(String title) {
surveyTitle = title;
}
public static int generateRespondentId() {
rID = ++respondentID;
return rID;
}
// displays name of survey and entire grid of results
public void displaySurveyResults() {
System.out.printf("%s\n\n", getTitle());
logResponse();
}
// dispalys question number and results for that question so far
public void displayQuestionStats(int questionNumber) {
}
// enter questions and store in an array of Strings
public void enterQuestions() {
/*String[] questions = {
"How would you rate your online shopping experience?",
"How satisfied was you with the purchase price?",
"Overall how was the online checkout experience?",
"How likely are you to recommend your friends and family to our store?",
"How concerned are you with online credit card security?",
"How likely are you to prefer a retail location compared to an online store?",
};*/
String questions[] = new String[10];
for (int i = 0; i < questions.length; i++) {
System.out.println("Please enter a question!");
questions[i] = in.nextLine();
}
/*TEST TEST***
System.out.print(questions[0] + "\n");
System.out.print(questions[1] + "\n");
System.out.print(questions[2] + "\n");
System.out.print(questions[3] + "\n");
System.out.print(questions[4] + "\n");
System.out.print(questions[5] + "\n");
System.out.print(questions[6] + "\n");
System.out.print(questions[7] + "\n");
System.out.print(questions[8] + "\n");
System.out.print(questions[9] + "\n");
*/
}
**// enters the response in the correct grid
public void logResponse() {
System.out.println("The responses are:\n");
System.out.print(" "); // align column heads
// create a column heading for each question
for (int qNumber = 0; qNumber < responses[0].length; qNumber++) {
System.out.printf("Question number %d ", qNumber + 1);
System.out.println("Response"); // column heading
}
for (int response = 0; response < responses.length; response++) {
System.out.printf("Response %2d", response + 1);
for (int qNumber : responses[response])// output responses
{
System.out.printf("%8d", qNumber);
}
}
}**
}
Probably you want the length of your array and not the length of your first element:
for (int qNumber = 0; qNumber < responses.length; qNumber++) {
System.out.printf("Question number %d ", qNumber + 1);
System.out.println("Response"); // column heading
}
I didn't initialize my array properly I did
private int[][] responses;
and it should have been
private int[][] responses = new int[5][11];
public class For {
public static void main(String[] args){
for(int i=2; i<=1024; i *= 2){
System.out.println("Count is: " + i);
}
}
public class While {
public static void main(String[] args){
int i = 1;
while (i < 1024) {
i *= 2;
System.out.println("Count is: " + i);
}
}
public class DoWhile {
public static void main(String[] args){
int i = 1;
if (i < 1024) {
do { i*=2;
System.out.println("Count is: " + i);
} while (i < 1024);
}
}
How would one convert the for loop/while loop so it does the same thing, but using a recursive function?
Like so:
public class Recursive {
public void r(int i) {
if (i < 1024) {
i *= 2;
System.out.println("Count is: " + i);
r(i);
}
}
public static void main(String[] args) {
Recursive r = new Recursive();
r.r(1);
}
}
Take the loop of main and put it in its own function with an argument int i. In that function, rewrite the loop to
If the loop condition is false (i >= 1024), then return
Else, recursive call with argument i*2.
Call the function with argument 1 or 2, depending on which of your programs you're rewriting (they don't entirely match).
Recurrent loop can look like this:
class Main
{
public static void main(String[] args){
RecWhile(1);
}
public static void RecWhile(int i) {
if (i < 1024) {
i = i*2;
System.out.println("Count is: " + i);
RecWhile(i);
}
}
}
public class Test1 {
public static void main(String[] args) {
Test1 mainFunc = new Test1();
int[] arr = {1,2,4,3,5,6};
int start=0;
int end=arr.length;
mainFunc.callRecursiveFun(start, end, arr);
}
public int callRecursiveFun(int start, int end, int[] arr) {
int arrLen = end;
if(arrLen == 0) {
return 0;
} else {
System.out.println("Loop Index at "+start +": "+arr[start]);
}
return callRecursiveFun(start+1, end-1, arr);
}
}