0% found this document useful (0 votes)
122 views2 pages

Neocolab

The document contains Java code that defines a main class with a custom class and a product class. It takes user input for a name and a number, calculates the cube of the number, and displays a greeting using the custom class. Additionally, it allows for the input of multiple products, sorts them by price in descending order, and prints the sorted product list.

Uploaded by

yuvkumar541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

Neocolab

The document contains Java code that defines a main class with a custom class and a product class. It takes user input for a name and a number, calculates the cube of the number, and displays a greeting using the custom class. Additionally, it allows for the input of multiple products, sorts them by price in descending order, and prints the sorted product list.

Uploaded by

yuvkumar541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;
class Main {
// You are using Java
static class CustomClass {
private String name;
public CustomClass(String name){
this.name = name;
}

public void display(){


System.out.println("CustomClass: Hello, "+name+"!");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int a = sc.nextInt();
System.out.println("Name is an instance of String: "+(name instanceof String));
System.out.println("Cube of the entered number: "+ a*a*a);
CustomClass customObj = new CustomClass(name);
customObj.display();
System.out.println("CustomObj is an instance of CustomClass: "+(customObj
instanceof CustomClass));
}
}

int n = Integer.parseInt(scanner.nextLine().trim());

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = Integer.parseInt(scanner.nextLine().trim()); // Ensuring no extra space is


included in input

Product[] products = new Product[n];

for (int i = 0; i < n; i++) {


String[] input = scanner.nextLine().split(" ", 2);
String name = input[0];
double price = Double.parseDouble(input[1]);
products[i] = new Product(name, price);
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (PriceComparator.compare(products[j], products[j + 1]) < 0) {
Product temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}

for (Product product : products) {


System.out.println(product);
}

scanner.close();
}
}

// You are using Java


class Product {
//Type your code here
public String name;
public double price;

public Product(String name, double price){


this.name = name;
this.price = price;
}

public String toString(){


return "Product Name: " + name + ", Price: Rs. " + price;
}
}

class PriceComparator {
//Type your code here
public static int compare(Product a,Product b){
return Double.compare(b.price,a.price);
}
}

You might also like