Why isn’t this compiling, even when I put the public static void main(String[] a
ID: 3908762 • Letter: W
Question
Why isn’t this compiling, even when I put the public static void main(String[] arg) it won’t compile?
full code:
public class CollegeInfo {
String college_name;
int sticker_price;
int net_price;
int discount;
//getter and setters of variables of class
public String getCollege_name() {
return college_name;
}
public void setCollege_name(String college_name) {
this.college_name = college_name;
}
public int getSticker_price() {
return sticker_price;
}
public void setSticker_price(int sticker_price) {
this.sticker_price = sticker_price;
}
public int getNet_price() {
return net_price;
}
public void setNet_price(int net_price) {
this.net_price = net_price;
}
//calculating discount provided by college
public void discount(){
discount=(int)((sticker_price-net_price)/sticker_price*100);
}
//returning the discount
public int getDiscount() {
return discount;
}
}
import java.util.Scanner;
public class MainClass {
//static members created to hold values which should not change with new object creation
static int avg_stick_price;//to hold average sticker price
static int avg_net_price;//to hold average net price
static int avg_discount;//to hold the discount
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of colleges to be entered: ");
int n=sc.nextInt();//asking user to provide length of array
CollegeInfo[] college_array=new CollegeInfo[n];//creating college array
for(int i=0;i<n;i++)//running loop till n
{
college_array[i]=new CollegeInfo();//assigning array term class
System.out.println("Enter Name of College: ");
college_array[i].setCollege_name(sc.next());//using setters to enter values
System.out.println("Enter Sticker Price: ");
college_array[i].setSticker_price(sc.nextInt());
avg_stick_price=avg_stick_price+college_array[i].getSticker_price();//using getters to calculate total for calculating average
System.out.println("Enter Net Price: ");
college_array[i].setNet_price(sc.nextInt());
avg_net_price=avg_net_price+college_array[i].getNet_price();
college_array[i].discount();
avg_discount=avg_discount+college_array[i].getDiscount();
}
System.out.format("%50s %16s %16s %16s ","College","Sticker Price","Net Price","% Discount Rate");
avg_stick_price=avg_stick_price/n;
avg_net_price=avg_net_price/n;
avg_discount=avg_discount/n;
//printing the result
for(int j=0;j<n;j++)
{
if(college_array[j].getDiscount()>=60)
{
//using string formatting
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"**","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}else if(college_array[j].getDiscount()>=50)
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"*","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}else
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name(),"$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}
}
System.out.format("%50s %16s %16s %16s","Average","$"+avg_stick_price,"$"+avg_net_price,avg_discount);
}
}
2 3 import java.util.Scanner 5 public class CollegeInfo 6 String college name; int sticker price; int net price; int discount; 9 10 12 13 14 156 16 17 18 19 20 21 226 23 24 25 26 27 28 29- 30 //getter and setters of variables of class public String getCollege name() return college.name; public void setcollege_name (String college_name) this.college name college name public int getsticker_price() Problems e Javadoc Declaration Console 3 Tasks terminated> Collegeinfo Java Application] C:lProgram FilesJavaljre1,8.0.144 bin javaw.exe (Oun 24 2018, 716:10 PM) Error: Main method not found in class CollegeInfo, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.ApplicationExplanation / Answer
Put CollegeInfo class within MainClass i.e; within MainClass not in main method. IT will compile successfully.
import java.util.Scanner;
import java.io.*;
public class MainClass {
public class CollegeInfo {
String college_name;
int sticker_price;
int net_price;
int discount;
//getter and setters of variables of class
public String getCollege_name() {
return college_name;
}
public void setCollege_name(String college_name) {
this.college_name = college_name;
}
public int getSticker_price() {
return sticker_price;
}
public void setSticker_price(int sticker_price) {
this.sticker_price = sticker_price;
}
public int getNet_price() {
return net_price;
}
public void setNet_price(int net_price) {
this.net_price = net_price;
}
//calculating discount provided by college
public void discount(){
discount=(int)((sticker_price-net_price)/sticker_price*100);
}
//returning the discount
public int getDiscount() {
return discount;
}
}
//static members created to hold values which should not change with new object creation
static int avg_stick_price;//to hold average sticker price
static int avg_net_price;//to hold average net price
static int avg_discount;//to hold the discount
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of colleges to be entered: ");
int n=sc.nextInt();//asking user to provide length of array
CollegeInfo[] college_array=new CollegeInfo[n];//creating college array
for(int i=0;i<n;i++)//running loop till n
{
college_array[i]=new CollegeInfo();//assigning array term class
System.out.println("Enter Name of College: ");
college_array[i].setCollege_name(sc.next());//using setters to enter values
System.out.println("Enter Sticker Price: ");
college_array[i].setSticker_price(sc.nextInt());
avg_stick_price=avg_stick_price+college_array[i].getSticker_price();//using getters to calculate total for calculating average
System.out.println("Enter Net Price: ");
college_array[i].setNet_price(sc.nextInt());
avg_net_price=avg_net_price+college_array[i].getNet_price();
college_array[i].discount();
avg_discount=avg_discount+college_array[i].getDiscount();
}
System.out.format("%50s %16s %16s %16s ","College","Sticker Price","Net Price","% Discount Rate");
avg_stick_price=avg_stick_price/n;
avg_net_price=avg_net_price/n;
avg_discount=avg_discount/n;
//printing the result
for(int j=0;j<n;j++)
{
if(college_array[j].getDiscount()>=60)
{
//using string formatting
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"**","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}else if(college_array[j].getDiscount()>=50)
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"*","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}else
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name(),"$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}
}
System.out.format("%50s %16s %16s %16s","Average","$"+avg_stick_price,"$"+avg_net_price,avg_discount);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.