I am new to coding.Though the following may think irrelevant please help me solve this. I have written the code in two different ways I want to know the complexity and runtime and which will give the faster result with Big-O notation between the two.I know that it can be written using 10 - 20 lines but I want to know the time complexity of the change in the length of the code.
Just a correction, every O(1) algorithm is not equal in execution time just because they are both O(1). Then how do we determine the execution time as they both are O(1)?
CODE1:
package can;
import java.util.Scanner;
public class scan2
{
private static Scanner scanner;
public static void main(String[] args)
{
System.out.println
("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples less than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");
int i;
int j =0;
float k = 0f;
scanner = new Scanner(System.in);
i = (scanner.nextInt());
if(i<=3&&i>0)
{
System.out.println("Please enter the quantity ");
scanner = new Scanner(System.in);
j = (scanner.nextInt());
}
else
{
System.out.println("Please choose a valid option ");
}
if (i == 1&&i>0)
{
{
System.out.println("you have entered "+j+" quantites of apples");
}
if (j<10&&j>0)
{
k = j*50;
System.out.println("Total cost is "+k);
}
else if(j>=10&&j>0)
{
k = j*40;
System.out.println("Total cost is "+k);
}
if(j>0)
{
System.out.println("Your shopping is completed");
}
else
{
System.out.println("Please enter valid quantity");
}
}
if (i == 2)
{
{
System.out.println("you have entered "+j+" quantites of bananas");
}
if (j<24&&j>0)
{
k = j*2;
System.out.println("Total cost is "+k);
}
else if(j>=24&&j>0)
{
k = j*1.5f;
System.out.println("Total cost is "+k);
}
if(j>0)
{
System.out.println("Your shopping is completed");
}
else
{
System.out.println("Please enter valid quantity");
}
}
if (i == 3)
{
{
System.out.println("you have entered "+j+" quantites of oranges");
}
if (j<12&&j>0)
{
k = j*5.7f;
System.out.println("Total cost is "+k);
}
else if(j>=12&&j>0)
{
k = j*4;
System.out.println("Total cost is "+k);
}
if(j>0)
{
System.out.println("Your shopping is completed");
}
else
{
System.out.println("Please enter valid quantity");
}
}
}
}
CODE2:
package can;
import java.util.Scanner;
public class shop
{
private static Scanner scanner;
public static void main(String[] args)
{
System.out.println
("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples less than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");
int i;
int j =0;
float k = 0f;
scanner = new Scanner(System.in);
i = (scanner.nextInt());
if(i<=3&&i>0)
{
System.out.println("Please enter the quantity ");
scanner = new Scanner(System.in);
j = (scanner.nextInt());
}
else
{
System.out.println("Please choose a valid option ");
}
if (i == 1&&i>0)
{
{
System.out.println("you have entered "+j+" quantites of apples");
}
if (j<10&&j>0)
{
k = j*50;
System.out.println("Total cost is "+k);
}
else if(j>=10&&j>0)
{
k = j*40;
System.out.println("Total cost is "+k);
}
}
if (i == 2)
{
{
System.out.println("you have entered "+j+" quantites of bananas");
}
if (j<24&&j>0)
{
k = j*2;
System.out.println("Total cost is "+k);
}
else if(j>=24&&j>0)
{
k = j*1.5f;
System.out.println("Total cost is "+k);
}
}
if (i == 3)
{
{
System.out.println("you have entered "+j+" quantites of oranges");
}
if (j<12&&j>0)
{
k = j*5.7f;
System.out.println("Total cost is "+k);
}
else if(j>=12&&j>0)
{
k = j*4;
System.out.println("Total cost is "+k);
}
}
if(i<=3&&j>0)
{
System.out.println("Your shopping is completed");
}
else if(j<0)
{
System.out.println("Please enter valid quantity");
}
else if(i<=3&&j == 0)
{
System.out.println("Please enter valid quantity");
}
}
}